qdapTools (version 1.3.3)

loc_split: Split Data Forms at Specified Locations

Description

Split data forms at specified integer locations.

Usage

loc_split(x, locs, names = NULL, ...)

# S3 method for list loc_split(x, locs, names = NULL, ...)

# S3 method for data.frame loc_split(x, locs, names = NULL, ...)

# S3 method for matrix loc_split(x, locs, names = NULL, ...)

# S3 method for numeric loc_split(x, locs, names = NULL, ...)

# S3 method for factor loc_split(x, locs, names = NULL, ...)

# S3 method for character loc_split(x, locs, names = NULL, ...)

# S3 method for default loc_split(x, locs, names = NULL, ...)

Arguments

x

A data form (list, vector, data.frame, matrix).

locs

A vector of integer locations to split at. If locs contains the index 1, it will be silently dropped.

names

Optional vector of names to give to the list elements.

Ignored.

Value

Returns of list of data forms broken at the locs.

See Also

run_split, split_vector https://github.com/trinker/loc_split_example for practical usage.

Examples

Run this code
# NOT RUN {
## character
loc_split(LETTERS, c(4, 10, 16))
loc_split(LETTERS, c(4, 10, 16), c("dog", "cat", "chicken", "rabbit"))

## numeric
loc_split(1:100, c(33, 66))

## factor
(p_chng <- head(1 + cumsum(rle(as.character(CO2[["Plant"]]))[[1]]), -1))
loc_split(CO2[["Plant"]], p_chng)

## list
loc_split(as.list(LETTERS), c(4, 10, 16))

## data.frame
(vs_change <- head(1 + cumsum(rle(as.character(mtcars[["vs"]]))[[1]]), -1))
loc_split(mtcars, vs_change)

## matrix
(mat <- matrix(1:50, nrow=10))
loc_split(mat, c(3, 6, 10))
# }

Run the code above in your browser using DataCamp Workspace