title: R: How to sum multiple columns of data frames in a list?
body: <p>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:</p>
  
  <pre><code>ls &lt;- 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
  </code></pre>
  
  <p>my expected result is:</p>
  
  <pre><code>ls2
  [[1]]
    c new
  1 3   8
  
  [[2]]
    c new
  1 7  11
  </code></pre>
  
  <p>Any ideas how to do this? So far I tried to enhance this <a href="https://stackoverflow.com/questions/31461357/sum-of-two-columns-of-data-frame-with-na-values">answer</a> for lists, without success and without omiting the input columns (a,b,d). I tried so far lapply:</p>
  
  <pre><code>lapply(ls, function(x) x$e &lt;- rowSums(x[,c("a", "b", "d")], na.rm=T)) 
  and 
  ls$e &lt;- lapply(ls, function(x) rowSums(x[,c("a", "b", "d")], na.rm=T)) 
  </code></pre>
  
  <p>Thank you in advance</p>
  
  <p><strong>Edit</strong>:
  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.</p>
  
  <p>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:</p>
  
  <pre><code>Error in rowSums(x[, columns_to_sum], na.rm = T) : 
   'x' must be an array of at least two dimensions"
  </code></pre>
  
  <p>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? </p>