Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum elements across a list of data.frames

I have a list(Total) of 12 data.frames (all the same form) and I need the sum of every row of the list in colum 2.

I'm searching for a smarter way to sum the elements across the list like this:

Total[[1]][,2] + Total[[2]][,2] + Total[[3]][,2] +..+Total[[12]][,2]

colum 2 has a length of 70 so the result should be a vector of length 70

hope someone knows he right sapply, lapply or apply function code

like image 554
7660 Avatar asked Apr 26 '17 08:04

7660


1 Answers

Here's an option:

Reduce("+", lapply(Total, "[[", 2))

Note however, that this doesn't play well with potential NAs because of +.

Here's an example with built-in data:

Reduce("+", lapply(list(iris, iris, iris), "[[", 2))
like image 157
talat Avatar answered Sep 29 '22 14:09

talat