The R Project SVN R

Rev

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