I have a data.table like this
dput(DT)
structure(list(ref = c(3L, 3L, 3L, 3L), nb = 12:15, i1 = c(3.1e-05,
0.044495, 0.82244, 0.322291), i2 = c(0.000183, 0.1
The dplyr
solution would be to use mutate_
together with paste(listCol, collapse = "+")
. But I guess the Reduce
solution is faster.
DT <- mutate_(DT, sum = paste(listCol, collapse = "+"))
You may also try with Reduce
DT[, Sum := Reduce(`+`, .SD), .SDcols=listCol][]
# ref nb i1 i2 i3 i4 Sum
#1: 3 12 0.000031 0.000183 0.000824 0.044495 0.045533
#2: 3 13 0.044495 0.155732 0.533939 0.822440 1.556606
#3: 3 14 0.822440 0.873416 0.838542 0.322291 2.856689
#4: 3 15 0.322291 0.648545 0.990648 0.393595 2.355079
NOTE: If there are "NA" values, it should be replaced with '0' before Reduce
i.e.
DT[, Sum := Reduce(`+`, lapply(.SD, function(x) replace(x,
which(is.na(x)), 0))), .SDcols=listCol][]
**Another solution :**using rowSums
DT[, Sum := rowSums(.SD, na.rm = TRUE), .SDcols = grep("i", names(DT))]
Use .SDcols
to specify the columns, then take rowSums
. Use :=
to assign new columns:
DT[ ,sum := rowSums(.SD), .SDcols = grep("i", names(DT))]