Learn R Programming

lambda.tools (version 1.0.9)

segment: Segment a sequence into shifted versions of itself

Description

Create a shifted version of a sequence to make it easier to do certain types of analysis.

Arguments

x
A vector
do.pad
Whether the vector should be padded to contain the edges of the sequence

Value

The return value is a data.frame with dimensions length(x) - 1 by 2 or length(x) + 1 by 2 if do.pad == TRUE. A data.frame is used to support arbitrary types. For example, using a Date vector will result in a numeric output, which is inconvenient.

Usage

segment(x, do.pad=FALSE)

Details

Segmenting sequences into offset versions of itself is useful for detecting patterns in sequences. This approach is compatible with a functional programming style as each row can then be passed to a map-vectorized function for further processing. The advantage over an iterative approach is that the map-vectorized function can focus on a row-specific model independent of data management mechanics like maintaining proper indices while iterating over the sequence, as this is handled by segment.

See Also

partition maprange

Examples

Run this code
segment(1:10)

# Notice how the ends of the sequence are given their own rows
segment(1:10, TRUE)

# Emulate segment using partition
partition(1:10, function(x) x, 1)

# Emulate segment using maprange
t(maprange(1:10, 2, function(x) x))

# Create four shifted copies instead of two
maprange(1:10, 4, function(x) x)

Run the code above in your browser using DataLab