
Last chance! 50% off unlimited learning
Sale ends in
by
is an object-oriented wrapper for
tapply
applied to data frames.
by(data, INDICES, FUN, ..., simplify = TRUE)
nrow(data)
.data
.FUN
.tapply
."by"
, giving the results for each subset.
This is always a list if simplify
is false, otherwise a list or
array (see tapply
).
FUN
is applied to each subset in turn. For the default method, an object with dimensions (e.g., a matrix) is
coerced to a data frame and the data frame method applied. Other
objects are also coerced to a data frame, but FUN
is applied
separately to (subsets of) each column of the data frame.
tapply
, simplify2array
.
ave
also applies a function block-wise.
require(stats)
by(warpbreaks[, 1:2], warpbreaks[,"tension"], summary)
by(warpbreaks[, 1], warpbreaks[, -1], summary)
by(warpbreaks, warpbreaks[,"tension"],
function(x) lm(breaks ~ wool, data = x))
## now suppose we want to extract the coefficients by group
tmp <- with(warpbreaks,
by(warpbreaks, tension,
function(x) lm(breaks ~ wool, data = x)))
sapply(tmp, coef)
Run the code above in your browser using DataLab