R: Split Data Forms at Specified Locations

loc_split {qdapTools} | R Documentation

## 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, ...)

    ## Default S3 method:
    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`.

### Note

Two dimensional object will retain dimension (i.e., `drop = FALSE` is used).

### See Also

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

### Examples

    ## 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))

[Package qdapTools version 1.3.3 Index]