More concise way to sum elements of columns in R Is there a more efficient/concise way to add the elements of these columns? Basically the D-# codes are error code for documents, and the Q# codes are classes of error.

##Summary columns##
errorarray$Q1 &lt;- errorarray$"D-03" + errorarray$"D-06" + errorarray$"D-07" + errorarray$"D-14" + errorarray$"D-15" + errorarray$"D-16"   
errorarray$Q2 &lt;- errorarray$"D-01" + errorarray$"D-02" + errorarray$"D-04" + errorarray$"D-05"
errorarray$Q3 &lt;- errorarray$"D-08" + errorarray$"D-09" + errorarray$"D-10" + errorarray$"D-11" + errorarray$"D-12" + errorarray$"D-13" + errorarray$"D-17" + errorarray$"D-18" +  errorarray$"D-19"


Thank you
Assuming your data are numeric and your columns are ordered by D-#, the following approaches should work:

Using apply:

errorarray$Q1 &lt;- apply(errorarray[, c(3, 6, 7, 14:16)], 1, sum) # using col indices
errorarray$Q1 &lt;- apply(errorarray[c("D-03", "D-06", "D-07", "D-15", "D-16")], 1, sum) # using column names


Using rowSums:

errorarray$Q1 &lt;- rowSums(errorarray[, c(3, 6, 7, 14:16)]) # using indices
errorarray$Q1 &lt;- rowSums(errorarray[c("D-03", "D-06", "D-07", "D-15", "D-16")]) # using column names


Note you can use ":" to denote the start and end of a number of consecutive columns.