简体   繁体   中英

R Data.Table Recode Many Columns

DATA=data.table(X1=c("Yes", "Maybe", "No", "Yes"), X7a=c("Yes", "Maybe", "No", "Yes"),
X1_A = c("Yes", NA, "No", "Yes"),
X1_B = c(1,NA,0,1))
FIX=c('X1','X7')

I have 'DATA' which contains ~100 columns such as 'X1' and 'X7a' I first wish to create a vector to store the column that I wish to recode such as FIX=c('X1','X7'). Then I wish to use data.table to recode the columns in 'FIX' with this rule:

for X1_A for example leave X1 as it is but replace "Maybe" with NA.

for X1_B, for example recode X1 so Yes = 1, No = 0, and Maybe is NA

How's this? Feels like there should be a better way where you join it to a table of your rules, but I can't quite see how to do that without doing 100 joins or having a table for every possible permutation of the 100 variables.

DATA=data.table(X1=c("Yes", "Maybe", "No", "Yes"), X7=c("Yes", "Maybe", "No", "Yes"))
FIX = c("X1", "X7") #or names(DATA)

style_A_function = function(x) fifelse(x == "Maybe", NA_character_, x)
style_B_function = function(x) fifelse(x == "Yes", 1, fifelse(x == "No", 0, fifelse(x == "Maybe", NA_integer_, 99)))

DATA[, paste0(FIX, c('_A')) := style_A_function(get(FIX))]
DATA[, paste0(FIX, c('_B')) := style_B_function(get(FIX))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM