split {base} | R Documentation |
split
divides the data in the vector x
into the groups
defined by f
. The assignment forms replace values
corresponding to such a division. Unsplit reverses the efffect of
split
split(x, f)
split.default(x, f)
split.data.frame(x, f)
split(x, f) <- value
split.default(x, f) <- value
split.data.frame(x, f) <- value
unsplit(value, f)
x |
vector or data frame containing values to be divided into groups. |
f |
a “factor” such that |
value |
a list of vectors or data frames compatible with a
splitting of |
f
is recycled as necessary and if the length of x
is not
a multiple of the length of f
a warning is printed.
unsplit
works only with lists of vectors. The data frame
method can also be used to split a matrix into a list of matrices,
and the assignment form likewise, provided they are invoked
explicitly.
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 factor levels
given be f
. If f
is longer than x
some of these
will be of zero length. The assignment forms return their right hand side.
unsplit
returns a vector for which split(x, f)
equals
value
cut
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-scores by group
z <- unsplit(lapply(split(x, g), scale), g)
tapply(z, g, mean)
# or
z <- x
split(z, g) <- lapply(split(x, g), scale)
tapply(z, g, sd)
## Split a matrix into a list by columns
ma <- cbind(x = 1:10, y = (-4:5)^2)
split(ma, col(ma))
split(1:10, 1:2)