splitAsList: Divide a vector-like object into groups

Description Usage Arguments Details Value See Also Examples

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## 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, ...)

splitAsList(x, f, drop=FALSE, ...)

relistToClass(x)

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

...

Extra arguments passed to any of the first 3 split() methods will be passed to splitAsList() (see Details below).

Extra arguments passed to the last split() method will be passed to base::split() (see Details below).

Extra arguments passed to splitAsList() will be passed to the specific method selected by method dispatch.

Details

The first 3 split() methods just delegate to splitAsList().

The last split() method just does:

1

splitAsList() is an S4 generic function. It is the workhorse behind the first 3 split() methods above. It behaves like base::split() except that it returns a List derivative instead of an ordinary list. The exact class of this List derivative depends only on the class of x and can be obtained independently with relistToClass(x).

Note that relistToClass(x) is the opposite of elementType(y) in the sense that the former returns the class of the result of relisting (or splitting) x while the latter returns the class of the result of unlisting (or unsplitting) y. More formally, if x is an object that is relistable and y a list-like object:

1
2
    relistToClass(x) is class(relist(x, some_skeleton))
    elementType(y) is class(unlist(y))

Therefore, for any object x for which relistToClass(x) is defined and returns a valid class, elementType(new(relistToClass(x))) should return class(x).

Value

splitAsList() and the first 3 split() methods behave like base::split() except that they return a List derivative (of class relistToClass(x)) instead of an ordinary list. Like with base::split(), all the list elements in this object have the same class as x.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## 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)

## Use splitAsList() if you need to split an ordinary vector into a
## List object:
split(letters, 1:2)        # ordinary list
splitAsList(letters, 1:2)  # List object

S4Vectors documentation built on Dec. 11, 2020, 2:02 a.m.