by {base} | R Documentation |
Apply a Function to a Data Frame split by Factors
Description
Function by
is an object-oriented wrapper for
tapply
applied to data frames.
Usage
by(data, INDICES, FUN, ...)
Arguments
data |
an R object, normally a data frame, possibly a matrix. |
INDICES |
a factor or a list of factors, each of length |
FUN |
a function to be applied to data frame subsets of |
... |
further arguments to |
Details
A data frame is split by row into data frames
subsetted by the values of one or more factors, and function
FUN
is applied to each subset in term.
Object data
will be coerced to a data frame by default.
Value
A list of class "by"
, giving the results for each subset.
See Also
tapply
Examples
data(warpbreaks)
attach(warpbreaks)
by(warpbreaks[, 1:2], tension, summary)
by(warpbreaks[, 1], list(wool=wool, tension=tension), summary)
by(warpbreaks, tension, function(x) lm(breaks ~ wool, data=x))
detach("warpbreaks")