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 <- errorarray$D-03 + errorarray$D-06 + errorarray$D-07 + errorarray$D-14 + errorarray$D-15 + errorarray$D-16   errorarray$Q2 <- errorarray$D-01 + errorarray$D-02 + errorarray$D-04 + errorarray$D-05errorarray$Q3 <- 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-19Thank you
More concise way to sum elements of columns in R
array;r
Assuming your data are numeric and your columns are ordered by D-#, the following approaches should work:Using apply:errorarray$Q1 <- apply(errorarray[, c(3, 6, 7, 14:16)], 1, sum) # using col indiceserrorarray$Q1 <- apply(errorarray[c(D-03, D-06, D-07, D-15, D-16)], 1, sum) # using column namesUsing rowSums:errorarray$Q1 <- rowSums(errorarray[, c(3, 6, 7, 14:16)]) # using indiceserrorarray$Q1 <- rowSums(errorarray[c(D-03, D-06, D-07, D-15, D-16)]) # using column namesNote you can use : to denote the start and end of a number of consecutive columns. 