Search for means and medians by data frames in r

I have several data frames, a b c d , each with the same column names. I want to find the average and median of these data frames. In other words, create 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'm sure this can be done with the built-in R functions, which will be faster.

+4
source share
3 answers

After Josh Ulrich answer, how about

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

? (Using rowMeans on the appropriate fragment will still be faster than apply ing mean ... I think there are rowMedians in the Biobase (Bioconductor) package if you really need speed?)

+9
source

I am not sure if the JD answer gives you exactly what you want, since the resulting object will not have the same dimensions as a , b , etc.

Putting your data in frames is a good start. Then you can multiply each column into a new list, cbind , which will enumerate into the matrix, and use apply on top of it.

 a <- data.frame(rnorm(10), runif(10)) b <- data.frame(rnorm(10), runif(10)) c <- data.frame(rnorm(10), runif(10)) d <- data.frame(rnorm(10), runif(10)) myList <- list(a,b,c,d) sapply(1:ncol(a), function(j) { # median apply(do.call(cbind,lapply(myList,`[`,,j)), 1, median) }) sapply(1:ncol(a), function(j) { # mean apply(do.call(cbind,lapply(myList,`[`,,j)), 1, mean) }) sapply(1:ncol(a), function(j) { # faster mean rowMeans(do.call(cbind,lapply(myList,`[`,,j))) }) 
+2
source

you can put your data frames in a list of data frames and then use lapply(myList, mean, ...)

+1
source

Source: https://habr.com/ru/post/1332938/


All Articles