I am trying to perform a function across dataframes cell-wise (not column or row-wise).
df1 <- data.frame(a=c(1,1,1),b=c(2,2,2))
df2 <- data.frame(a=c(3,3,3),b=c(4,4,4))
df3 <- data.frame(a=c(5,5,5),b=c(6,6,6))
>> df1
a b
1 1 2
2 1 2
3 1 2
>> df2
a b
1 3 4
2 3 4
3 3 4
>> df3
a b
1 5 6
2 5 6
3 5 6
Say, I want to compute the sum cell-wise (adding up values in each cell across all dataframes), so for the first cell, top-left would be (1+3+5=9). The full result would be this:
a b
1 9 13
2 9 13
3 9 13
And I have loads of data.frames stored in a list. I am looking for a base R solution.
This is what I have so far. I think I am pretty close. I created 3-dimensional arrays out of them.
a <- simplify2array(lapply(list(df1,df2,df3),as.matrix))
>> a
, , 1
a b
[1,] 1 2
[2,] 1 2
[3,] 1 2
, , 2
a b
[1,] 3 4
[2,] 3 4
[3,] 3 4
, , 3
a b
[1,] 5 6
[2,] 5 6
[3,] 5 6
>> str(a)
num [1:3, 1:2, 1:3] 1 1 1 2 2 2 3 3 3 4 ...
- attr(*, "dimnames")=List of 3
..$ : NULL
..$ : chr [1:2] "a" "b"
..$ : NULL
And then I tried to apply over the third dimension, but this doesn't work.
apply(a,3,sum)
Perhaps I need to use aperm
. I have experimented around this, but I don't understand how it works!
aperm(a, c(3,1,2))