Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to combine mean and sd in one table using tapply?

Tags:

r

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.

like image 575
user25260 Avatar asked Dec 04 '22 11:12

user25260


1 Answers

Here are two approaches and a few variations of each:

  1. In the first approach we use a function that returns both mean and sd.

  2. 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)))
like image 156
G. Grothendieck Avatar answered Apr 30 '23 23:04

G. Grothendieck