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
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With