Learn R Programming

qdapTools (version 1.1.0)

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 class 'list':
loc_split(x, locs, names = NULL, ...)

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

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

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

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

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

## S3 method for class '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

split_vector, https://github.com/trinker/loc_split_example for real life use.

Examples

Run this code
## 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 DataLab