The R Project SVN R

Rev

Rev 61150 | Rev 70587 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/split.Rd
2
% Part of the R package, http://www.R-project.org
59645 ripley 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{split}
42744 pd 7
\title{Divide into Groups and Reassemble}
56186 murdoch 8
\alias{split}
13423 hornik 9
\alias{split.default}
10
\alias{split.data.frame}
18591 pd 11
\alias{split<-}
24170 ripley 12
\alias{split<-.default}
18591 pd 13
\alias{split<-.data.frame}
14
\alias{unsplit}
13423 hornik 15
\description{
16
  \code{split} divides the data in the vector \code{x} into the groups
38130 ripley 17
  defined by \code{f}.  The replacement forms replace values
18
  corresponding to such a division.  \code{unsplit} reverses the effect of
20933 maechler 19
  \code{split}.
13423 hornik 20
}
2 r 21
\usage{
34978 maechler 22
split(x, f, drop = FALSE, ...)
23
split(x, f, drop = FALSE, ...) <- value
34957 maechler 24
unsplit(value, f, drop = FALSE)
2 r 25
}
26
\arguments{
18591 pd 27
  \item{x}{vector or data frame containing values to be divided into groups.}
42961 ripley 28
  \item{f}{a \sQuote{factor} in the sense that \code{\link{as.factor}(f)}
34957 maechler 29
    defines the grouping, or a list of such factors in which case their
30
    interaction is used for the grouping.}
31
  \item{drop}{logical indicating if levels that do not occur should be dropped
32
    (if \code{f} is a \code{factor} or a list).}
18591 pd 33
  \item{value}{a list of vectors or data frames compatible with a
34957 maechler 34
    splitting of \code{x}. Recycling applies if the lengths do not match.}
34978 maechler 35
  \item{\dots}{further potential arguments passed to methods.}
2 r 36
}
20933 maechler 37
\details{
24194 ripley 38
  \code{split} and \code{split<-} are generic functions with default and
59645 ripley 39
  \code{data.frame} methods.  The data frame method can also be used to
40
  split a matrix into a list of matrices, and the replacement form
41
  likewise, provided they are invoked explicitly.
28633 ripley 42
 
42744 pd 43
  \code{unsplit} works with lists of vectors or data frames (assumed to
44
  have compatible structure, as if created by \code{split}). It puts
45
  elements or rows back in the positions given by \code{f}. In the data
46
  frame case, row names are obtained by unsplitting the row name
47
  vectors from the elements of \code{value}.
48
 
49
  \code{f} is recycled as necessary and if the length of \code{x} is not
50
  a multiple of the length of \code{f} a warning is printed.
51
 
28633 ripley 52
  Any missing values in \code{f} are dropped together with the
53
  corresponding values of \code{x}.
59645 ripley 54
 
59660 hornik 55
  The default method calls \code{\link{interaction}}.  If the levels of
56
  the factors contain \samp{.} they may not be split as expected, so
59645 ripley 57
  the method has argument \code{sep} which is use to join the levels.
7081 pd 58
}
59
\value{
18591 pd 60
  The value returned from \code{split} is a list of vectors containing
28633 ripley 61
  the values for the groups.  The components of the list are named by
44220 ripley 62
  the levels of \code{f} (after converting to a factor, or if already a
61150 ripley 63
  factor and \code{drop = TRUE}, dropping unused levels).
28633 ripley 64
 
38130 ripley 65
  The replacement forms return their right hand side.  \code{unsplit}
42744 pd 66
  returns a vector or data frame for which \code{split(x, f)} equals
67
  \code{value}
68
 
2 r 69
}
24194 ripley 70
\seealso{
51387 ripley 71
  \code{\link{cut}} to categorize numeric values.
72
 
73
  \code{\link{strsplit}} to split strings.
24194 ripley 74
}
24300 ripley 75
\references{
76
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
77
  \emph{The New S Language}.
47262 ripley 78
  Wadsworth & Brooks/Cole.
24300 ripley 79
}
2 r 80
\examples{
41508 ripley 81
require(stats); require(graphics)
2 r 82
n <- 10; nn <- 100
51387 ripley 83
g <- factor(round(n * runif(n * nn)))
12976 pd 84
x <- rnorm(n * nn) + sqrt(as.numeric(g))
48 hornik 85
xg <- split(x, g)
86
boxplot(xg, col = "lavender", notch = TRUE, varwidth = TRUE)
2 r 87
sapply(xg, length)
88
sapply(xg, mean)
544 pd 89
 
51752 ripley 90
### Calculate 'z-scores' by group (standardize to mean zero, variance one)
18591 pd 91
z <- unsplit(lapply(split(x, g), scale), g)
92
 
93
# or
94
 
51752 ripley 95
zz <- x
96
split(zz, g) <- lapply(split(x, g), scale)
97
 
98
# and check that the within-group std dev is indeed one
18591 pd 99
tapply(z, g, sd)
51752 ripley 100
tapply(zz, g, sd)
18591 pd 101
 
51752 ripley 102
 
42744 pd 103
### data frame variation
104
 
105
## Notice that assignment form is not used since a variable is being added
106
 
107
g <- airquality$Month
108
l <- split(airquality, g)
109
l <- lapply(l, transform, Oz.Z = scale(Ozone))
110
aq2 <- unsplit(l, g)
111
head(aq2)
61150 ripley 112
with(aq2, tapply(Oz.Z,  Month, sd, na.rm = TRUE))
42744 pd 113
 
61433 ripley 114
 
42744 pd 115
### Split a matrix into a list by columns
2 r 116
ma <- cbind(x = 1:10, y = (-4:5)^2)
117
split(ma, col(ma))
544 pd 118
 
119
split(1:10, 1:2)
2 r 120
}
286 maechler 121
\keyword{category}