mApply
is like tapply
except that the first argument can
be a matrix or a vector, and the output is cleaned up if simplify=TRUE
.
It uses code adapted from Tony Plate (As mApply
can be much faster than using by
, it is often
worth the trouble of converting a data frame to a numeric matrix for
processing by mApply
. asNumericMatrix
will do this, and
matrix2dataFrame
will convert a numeric matrix back into a data
frame if attributes and storage modes of the original variables are
saved by calling subsAttr
. subsAttr
saves attributes that
are commonly preserved across row subsetting (i.e., it does not save
dim
, dimnames
, or names
attributes).
mApply(X, INDEX, FUN, ..., simplify=TRUE)
FUN
argumentmApply
, the returned value is a vector, matrix, or list.
If FUN
returns more than one number, the result is an array if
simplify=TRUE
and is a list otherwise. If a matrix is returned,
its rows correspond to unique combinations of INDEX
. If
INDEX
is a list with more than one vector, FUN
returns
more than one number, and simplify=FALSE
, the returned value is a
list that is an array with the first dimension corresponding to the last
vector in INDEX
, the second dimension corresponding to the next
to last vector in INDEX
, etc., and the elements of the list-array
correspond to the values computed by FUN
. In this situation the
returned value is a regular array if simplify=TRUE
. The order
of dimensions is as previously but the additional (last) dimension
corresponds to values computed by FUN
.asNumericMatrix
, matrix2dataFrame
, tapply
,
sapply
, lapply
, mapply
, by
.require(datasets, TRUE)
a <- mApply(iris[,-5], iris$Species, mean)
Run the code above in your browser using DataCamp Workspace