[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  split: Divide into Groups and Reassemble

##  Description

` split ` divides the data in the vector ` x ` into the groups defined by ` f
` . The replacement forms replace values corresponding to such a division. `
unsplit ` reverses the effect of ` split ` .

##  Usage

    
    
    split(x, f, drop = FALSE, …)
    # S3 method for default
    split(x, f, drop = FALSE, sep = ".", lex.order = FALSE, …)
    
    split(x, f, drop = FALSE, …) <- value
    unsplit(value, f, drop = FALSE)

##  Arguments

x

vector or data frame containing values to be divided into groups.

f

a ‘factor’ in the sense that ` [ as.factor
](/link/as.factor?package=base&version=3.6.2) (f) ` defines the grouping, or a
list of such factors in which case their interaction is used for the grouping.

drop

logical indicating if levels that do not occur should be dropped (if ` f ` is
a ` factor ` or a list).

value

a list of vectors or data frames compatible with a splitting of ` x ` .
Recycling applies if the lengths do not match.

sep

character string, passed to ` [ interaction
](/link/interaction?package=base&version=3.6.2) ` in the case where ` f ` is a
` [ list ](/link/list?package=base&version=3.6.2) ` .

lex.order

logical, passed to ` [ interaction
](/link/interaction?package=base&version=3.6.2) ` when ` f ` is a list.

…

further potential arguments passed to methods.

##  Value

The value returned from ` split ` is a list of vectors containing the values
for the groups. The components of the list are named by the levels of ` f `
(after converting to a factor, or if already a factor and ` drop = TRUE ` ,
dropping unused levels).

The replacement forms return their right hand side. ` unsplit ` returns a
vector or data frame for which ` split(x, f) ` equals ` value `

##  Details

` split ` and ` split<- ` are generic functions with default and ` data.frame
` methods. The data frame method can also be used to split a matrix into a
list of matrices, and the replacement form likewise, provided they are invoked
explicitly.

` unsplit ` works with lists of vectors or data frames (assumed to have
compatible structure, as if created by ` split ` ). It puts elements or rows
back in the positions given by ` f ` . In the data frame case, row names are
obtained by unsplitting the row name vectors from the elements of ` value ` .

` f ` is recycled as necessary and if the length of ` x ` is not a multiple of
the length of ` f ` a warning is printed.

Any missing values in ` f ` are dropped together with the corresponding values
of ` x ` .

The default method calls ` [ interaction
](/link/interaction?package=base&version=3.6.2) ` when ` f ` is a ` [ list
](/link/list?package=base&version=3.6.2) ` . If the levels of the factors
contain  .  the factors may not be split as expected, unless ` sep ` is set to
string not present in the factor ` [ levels
](/link/levels?package=base&version=3.6.2) ` .

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ cut ](/link/cut?package=base&version=3.6.2) ` to categorize numeric
values.

` [ strsplit ](/link/strsplit?package=base&version=3.6.2) ` to split strings.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fsplit&utm_source=r-docs&utm_medium=docs&utm_term=split&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    require(stats); require(graphics)
    n <- 10; nn <- 100
    g <- factor(round(n * runif(n * nn)))
    x <- rnorm(n * nn) + sqrt(as.numeric(g))
    xg <- split(x, g)
    boxplot(xg, col = "lavender", notch = TRUE, varwidth = TRUE)
    sapply(xg, length)
    sapply(xg, mean)
    
    ### Calculate 'z-s