aaaaa
aaaaa

Reputation: 183

r - Overall mean of elements in list by rows and columns

I need to take the overall mean in each row and column within a list of data.frames:

set.seed(1)

df_1 = data.frame(a = rnorm(3), b = rnorm(3), c = rnorm(3))
df_2 = data.frame(a = rnorm(3), b = rnorm(3), c = rnorm(3))
df_3 = data.frame(a = rnorm(3), b = rnorm(3), c = rnorm(3))

df_lst = list(df_1, df_2, df_3)

Here I need to do the following:

mean(c(df_lst[[1]]$a[1], df_lst[[2]]$a[1], df_lst[[3]]$a[1]))
mean(c(df_lst[[1]]$a[2], df_lst[[2]]$a[2], df_lst[[3]]$a[2]))
mean(c(df_lst[[1]]$a[3], df_lst[[2]]$a[3], df_lst[[3]]$a[3]))

mean(c(df_lst[[1]]$b[1], df_lst[[2]]$b[1], df_lst[[3]]$b[1]))
mean(c(df_lst[[1]]$b[2], df_lst[[2]]$b[2], df_lst[[3]]$b[2]))
mean(c(df_lst[[1]]$b[3], df_lst[[2]]$b[3], df_lst[[3]]$b[3]))

mean(c(df_lst[[1]]$c[1], df_lst[[2]]$c[1], df_lst[[3]]$c[1]))
mean(c(df_lst[[1]]$c[2], df_lst[[2]]$c[2], df_lst[[3]]$c[2]))
mean(c(df_lst[[1]]$c[3], df_lst[[2]]$c[3], df_lst[[3]]$c[3]))

And the desired output is:

> out
            a          b         c
1 -0.03687367  0.5853922 0.3541071
2  0.76310860 -0.6035424 0.2220019
3  0.15773067 -0.5616297 0.4546074

Any suggestion?

Upvotes: 1

Views: 66

Answers (1)

akrun
akrun

Reputation: 887991

We can use Reduce to get the elementwise sum (+) and then divide by the length of the list

Reduce(`+`, df_lst)/length(df_lst)
#           a          b         c
#1 -0.03687367  0.5853922 0.3541071
#2  0.76310860 -0.6035424 0.2220019
#3  0.15773067 -0.5616297 0.4546074

Or convert it to an array and then use apply

apply(array(unlist(df_lst), c(3, 3, 3)), 1:2, mean)

Upvotes: 1

Related Questions