Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding means and medians across data frames in r

I have several data frames, a b c d, each with the same column names. I want to find the mean and median of those data frames. In other words, construct new mean and median data frames that are the same size as a, b, etc.

I could use a couple of for loops, but I bet there is a slick way of doing this using the R built-in functions that would be faster.

like image 245
tkerwin Avatar asked Dec 17 '22 19:12

tkerwin


1 Answers

Following Josh Ulrich's answer, how about

library(abind)
apply(abind(a,b,c,d,along=3),c(1,2),median)

? (Using rowMeans on the appropriate slice will still be faster than applying mean ... I think there is a rowMedians in the Biobase (Bioconductor) package if you really need speed?)

like image 113
Ben Bolker Avatar answered Jan 14 '23 07:01

Ben Bolker