slice: Slice a sequence into two adjacent sub-sequences

Description Arguments Value Usage Details Author(s) Examples

Description

A sequence can be sliced using an explicit pivot point or by using a logical expression.

Arguments

x

An indexable data structure, typically a vector

pivot

The index of the pivot point in x

inclusive

Whether to include the pivot point in the second sub-sequence

expression

A logical expression

Value

A list containing two sub-sequences or sub-matrices

Usage

slice(x, pivot, inclusive=FALSE)

slice(x, expression)

Details

This function splits a sequence into two adjacent sub-sequences at a pivot point or based on a logical expression. If a pivot point is chosen, then the inclusive parameter determines whether the value associated with the pivot should be included in both sub-sequences. If FALSE, then the indices of the sub-sequences will have the form [1, pivot], [pivot + 1, n], where n = |x|. If inclusive is TRUE, then the sub-sequences have indices of [1, pivot], [pivot, n]. Obviously the pivot must be an element of the set of indices of x.

An alternative construction is to use an expression to define a slice point. The first sub-sequence corresponds to the values where the expression evaluated to TRUE, while the second sequence corresponds to values when the expression evaluated to FALSE.

In two dimensions only the first variant of this function is defined, as it cannot be guaranteed that a regular matrix will be generated using an arbitrary expression.

Author(s)

Brian Lee Yung Rowe

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# The number 4 is included in each sub-sequence
x <- 1:10
slice(x, 4, TRUE)

# With expressions, the sub-sequences are not necessarily contiguous
slice(x, x %% 2 == 0)

# Same as above but in two dimensions
x <- matrix(1:40, ncol=4)
slice(x, 4)

zatonovo/lambda.tools documentation built on May 4, 2019, 9:11 p.m.