split_index: Split Data Forms at Specified Indices

View source: R/split_index.R

split_indexR Documentation

Split Data Forms at Specified Indices

Description

Split data forms at specified integer indices.

Usage

split_index(
  x,
  indices = if (is.atomic(x)) {
     NULL
 } else {
     change_index(x)
 },
  names = NULL,
  ...
)

## S3 method for class 'list'
split_index(x, indices, names = NULL, ...)

## S3 method for class 'data.frame'
split_index(x, indices, names = NULL, ...)

## S3 method for class 'matrix'
split_index(x, indices, names = NULL, ...)

## S3 method for class 'numeric'
split_index(x, indices = change_index(x), names = NULL, ...)

## S3 method for class 'factor'
split_index(x, indices = change_index(x), names = NULL, ...)

## S3 method for class 'character'
split_index(x, indices = change_index(x), names = NULL, ...)

## Default S3 method:
split_index(x, indices = change_index(x), names = NULL, ...)

Arguments

x

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

indices

A vector of integer indices to split at. If indices contains the index 1, it will be silently dropped. The default value when x evaluates to TRUE for is.atomic is to use change_index(x).

names

Optional vector of names to give to the list elements.

...

Ignored.

Value

Returns of list of data forms broken at the indices.

Note

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

See Also

change_index

Examples

## character
split_index(LETTERS, c(4, 10, 16))
split_index(LETTERS, c(4, 10, 16), c("dog", "cat", "chicken", "rabbit"))

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

## factor
(p_chng <- change_index(CO2[["Plant"]]))
split_index(CO2[["Plant"]], p_chng)
#`change_index` was unnecessary as it is the default of atomic vectors
split_index(CO2[["Plant"]])

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

## data.frame
(vs_change <- change_index(mtcars[["vs"]]))
split_index(mtcars, vs_change)

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

trinker/textshape documentation built on April 5, 2024, 11:39 a.m.