I have a data.table in R that's shown below:
I want to add a column that represents the sum of ace, bb, and statin for every row. So for the rows shown above, the new column would have the following values: 2,1,2,1,2
The name of the table above is adherence
, I tried the following code to add the sum:
adherence[, total := sum(ace,bb,statin)]
but instead of calculating the sum per row, it calculates the sum of all three columns and gives me the table below:
How do I get the sum of the three columns per row?
CodePudding user response:
The sum
calculate the entire sum of the columns. Instead, we need rowSums
(assuming the input dataset as data.table
, specify the columns to select in .SDcols
, and apply the rowSums
on the Subset of data.table (.SD
)
library(data.table)
adherence[, total := rowSums(.SD, na.rm = TRUE),
.SDcols = c("ace", "bb", "statin")][]
Or without specifying the .SDcols
adherence[, total := rowSums(.SD[, .(ace, bb, statin)], na.rm = TRUE)]
CodePudding user response:
Another solution. (Note that if any row contains a missing value, the row sum would be missing).
library(data.table)
adherence[, total := ace bb statin]