Reputation: 33
I have multiple columns in a data.table that I would like to apply an ifelse statement on. I could repeat the code for every column, but am wondering if there is a more elegant solution
For a given data table, I want to alter some numeric columns using the ifelse statement.
Here is the data:
dt = data.table(id = c(101, 102, 103, 104), one_cnt = c(0, 6, 4, 0), two_cnt = c(4, 0, 0, 2))
dt
If I wanted to do this manually, I could do this across all columns
dt$one_cnt = ifelse(dt$one_cnt == 0, 0, 2)
Instead, I'd like to select the following columns and apply the ifelse on each
colnames(dt)[grepl("cnt", colnames(dt))]
for one_cnt and two_cnt, a 0 value would end up with a 0. A non-zero value would take on a 1 value
Upvotes: 2
Views: 1245
Reputation: 389325
We could use .SDcols
to specify selected columns.
library(data.table)
cols <- grep("cnt", colnames(dt), value = TRUE)
dt[, (cols) := lapply(.SD, function(x) ifelse(x == 0, 0, 2)), .SDcols = cols]
dt
# id one_cnt two_cnt
#1: 101 0 2
#2: 102 2 0
#3: 103 2 0
#4: 104 0 2
From your attempt you are replacing 0 values with 0 and changing non-zero value to 2 but in your description you mentioned that you want to change the non-zero value to 1, so I am not clear about your expected output. If you want to change it to 1, you can do
dt[, (cols) := lapply(.SD, function(x) as.integer(as.logical(x))), .SDcols = cols]
Upvotes: 4