split-methods {S4Vectors} | R Documentation

## Divide a vector-like object into groups

### Description

`split` divides the data in a vector-like object `x` into the groups defined by `f`.

NOTE: This man page is for the `split` methods defined in the S4Vectors package. See `?base::split` for the default method (defined in the base package).

### Usage

    ## S4 method for signature 'Vector,ANY'
    split(x, f, drop=FALSE)

    ## S4 method for signature 'ANY,Vector'
    split(x, f, drop=FALSE)

    ## S4 method for signature 'Vector,Vector'
    split(x, f, drop=FALSE)

    ## S4 method for signature 'list,Vector'
    split(x, f, drop=FALSE, ...)

### Arguments

`x, f` | 2 vector-like objects of the same length. `f` will typically be a factor, but not necessarily.
`drop` | Logical indicating if levels that do not occur should be dropped (if `f` is a factor).
`...` | Arguments passed to `base::split` (see Details below).

### Details

The first 3 methods just delegate to the `IRanges::splitAsList` function defined in the IRanges package.

The method for `list` does:

        split(x, as.vector(f), drop=drop, ...)

### Value

All these methods behave like `base::split` except that the first 3 methods return a List object instead of an ordinary list.

### See Also

- The `split` function in the base package.

- The `splitAsList` function in the IRanges package.

- Vector and List objects.

- Rle and DataFrame objects.

### Examples

    ## On an Rle object:
    x <- Rle(101:105, 6:2)
    split(x, c("B", "B", "A", "B", "A"))

    ## On a DataFrame object:
    groups <- c("group1", "group2")
    DF <- DataFrame(
        a=letters[1:10],
        i=101:110,
        group=rep(factor(groups, levels=groups), c(3, 7))
    )
    split(DF, DF$group)

[Package S4Vectors version 0.22.1 Index]