N.Varela - 2 months ago 6
R Question

# R: How to sum multiple columns of data frames in a list?

i want to sum multiple columns of data frames in a list and only show the sum without showing the (calculation) input columns. Here an example:

``````ls <- list(data.frame(a=1, b=5, c=3, d=2), data.frame(a=NA, b=2, c=7, d=9))

ls
[[1]]
a b c d
1 1 5 3 2

[[2]]
a b c d
1 NA 2 7 9
``````

my expected result is:

``````ls2
[[1]]
c new
1 3   8

[[2]]
c new
1 7  11
``````

Any ideas how to do this? So far I tried to enhance this answer for lists, without success and without omiting the input columns (a,b,d). I tried so far lapply:

``````lapply(ls, function(x) x\$e <- rowSums(x[,c("a", "b", "d")], na.rm=T))
and
ls\$e <- lapply(ls, function(x) rowSums(x[,c("a", "b", "d")], na.rm=T))
``````

Edit:
Thanks Aech and Abdou for your answers, which work fine with this example. However, I have >200 columns, do you know a way without writing the columns that will remain? Like deleting the columns that I use for the calculation, instead of naming all columns.

EDIT 2:
Thanks for your improved code, it works well with the example data. However, with my true data set not... I get the following error:

``````Error in rowSums(x[, columns_to_sum], na.rm = T) :
'x' must be an array of at least two dimensions"
``````

My list has about 96 matrices with 200 columns and one row. But I donĀ“t know how to prepare a reproducible example of my error. Any ideas?

You should not name your list ls, because ls is a function.

``````lapply(myList, function(x) data.frame(c=x\$c, new = rowSums(x[,c("a", "b", "d")], na.rm=T)))
``````

Here is a solution where you specify the dropped columns only (after edit):

``````dropped <- c("a", "b", "d")
lapply(myList, function(x) {
x\$new <- rowSums(x[,dropped], na.rm=T)
x[!names(x) %in% dropped]
})
``````