I am trying to implement the rowsums solution proposed here Getting rowSums in a data table in R . Basically I want a variable with the sum of top15, top16 and top17 for each row. This output produces an answer but its clearly not right, I am sure I understand what is happening.
I am looking for a data.table solution - I am running this on millions of cases
library( data.table)
d <- structure(list(top15 = c(1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), top16 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0), top17 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0)), class = c("data.table",
"data.frame"), row.names = c(NA, -20L))
d[ , tops:=lapply(.SD,sum), .SDcols=c(paste0("top", 15:17))]