Rev 24300 | Rev 28633 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{split}\title{Divide into Groups}\alias{split}\alias{split.default}\alias{split.data.frame}\alias{split<-}\alias{split<-.default}\alias{split<-.data.frame}\alias{unsplit}\description{\code{split} divides the data in the vector \code{x} into the groupsdefined by \code{f}. The assignment forms replace valuescorresponding to such a division. Unsplit reverses the effect of\code{split}.}\usage{split(x, f)split(x, f) <- valueunsplit(value, f)}\arguments{\item{x}{vector or data frame containing values to be divided into groups.}\item{f}{a \dQuote{factor} such that \code{factor(f)} defines thegrouping, or a list of such factors in which case their interactionis used for the grouping.}\item{value}{a list of vectors or data frames compatible with asplitting of \code{x}}}\details{\code{split} and \code{split<-} are generic functions with default and\code{data.frame} methods.\code{f} is recycled as necessary and if the length of \code{x} is nota multiple of the length of \code{f} a warning is printed.\code{unsplit} works only with lists of vectors. The data framemethod can also be used to split a matrix into a list of matrices,and the assignment form likewise, provided they are invokedexplicitly.}\value{The value returned from \code{split} is a list of vectors containingthe values for thegroups. The components of the list are named by the factor levelsgiven be \code{f}. If \code{f} is longer than \code{x} some of thesewill be of zero length. The assignment forms return their right hand side.\code{unsplit} returns a vector for which \code{split(x, f)} equals\code{value}}\seealso{\code{\link{cut}}}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\examples{n <- 10; nn <- 100g <- 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 groupz <- unsplit(lapply(split(x, g), scale), g)tapply(z, g, mean)# orz <- xsplit(z, g) <- lapply(split(x, g), scale)tapply(z, g, sd)## Split a matrix into a list by columnsma <- cbind(x = 1:10, y = (-4:5)^2)split(ma, col(ma))split(1:10, 1:2)}\keyword{category}