I have 2 lists of dataframes in R (where the respective dataframes in each list are the same size). Is it possible to add the respective dataframnes in each list together in one line.
e.g. say we had
list1 <- list('a' = data.frame('x'=c(0,1),'y'=c(2,0)), 'b' = data.frame('x'=c(1,1),'y'=c(1,1)))
list2 <- list('a' = data.frame('x'=c(1,2),'y'=c(1,1)), 'b' = data.frame('x'=c(0,1),'y'=c(0,1)))
So list1 is:
$a
x y
0 2
1 0
$b
x y
1 1
1 1
And list2 is:
$a
x y
1 1
2 1
$b
x y
0 0
1 1
The final output would be:
$a
x y
1 3
3 1
$b
x y
1 1
2 2
Could obviously do this in two seperate lines by doing:
listOutput <- list()
listOutput[['a']] <- list1[['a']] + list2[['a']]
listOutput[['b']] <- list1[['b']] + list2[['b']]
but is there a simple way to do this in one line, maybe using lapply?
Thanks