The R Project SVN R

Rev

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