Home > OS >  Creating a new column that has the sum of multiple columns per row using data.table in R
Creating a new column that has the sum of multiple columns per row using data.table in R

Time:10-09

I have a data.table in R that's shown below:

enter image description here

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:

enter image description here

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]
  • Related