DT <- data.table(A = 1:5, B = 2:6, C = 3:7)
I want to make summation of 3 columns using column index:
DT[, D := do.call(sum, .SD), .SDcols = 1:3]
but code above doesnt work,
also I dont want to use DT[, D := (A+B+C)]
You can use rowSums
on .SD
:
DT[, D := rowSums(.SD), .SDcols = 1:3][]
# A B C D
#1: 1 2 3 6
#2: 2 3 4 9
#3: 3 4 5 12
#4: 4 5 6 15
#5: 5 6 7 18
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With