In R's tapply
function, is there an easy way to output multiple functions combined (e.g. mean
and sd
) in list form?
that is, output of:
tapply(x, factor, mean)
tapply(x, factor, sd)
to appear combined in one data frame.
Here are two approaches and a few variations of each:
In the first approach we use a function that returns both mean
and
sd
.
In the second approach we repeatedly call tapply
, once for mean
and then once for sd
.
We have used the iris
data set that comes with R so that this code runs:
1) First solution
# input data
x <- iris$Sepal.Length
factor <- iris$Species
### Solution 1
mean.sd <- function(x) c(mean = mean(x), sd = sd(x))
simplify2array(tapply(x, factor, mean.sd))
Here are two variations of the above solution. They use the same tapply
construct but simplify it using do.call
. The first gives a similar result to the solution above and the second is its transpose:
# Solution 1a - use same mean.sd
do.call("rbind", tapply(x, factor, mean.sd))
# Solution 1b - use same mean.sd - result is transposed relative to last two
do.call("cbind", tapply(x, factor, mean.sd))
2) Second solution. This is a second solution which gives a similar result as 1 and 1a above:
### Solution 2 - orientation is the same as 1 and 1a
mapply(tapply, c(mean = mean, sd = sd), MoreArgs = list(X = x, INDEX = factor))
This is the same as 2 except we transpose it at the end to correspond to 1b:
# Solution 2a - same as 2 except orientation is transposed so that it corresponds to 1b
t(mapply(tapply, c(mean = mean, sd = sd), MoreArgs = list(X = x, INDEX = factor)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With