Recode a variable using data.table package

南楼画角 提交于 2020-03-17 02:57:25

问题


If I want to recode a variable in R using data.table, what is the syntax? I saw some ans but didn't find them appropriate.

e.g. if I have the variable called gender

I want to recode gender 0 to unknown, 1 to male, 2 to female: here is how I tried:

Name <- c("John", "Tina", "Dave", "Casper")
Gender <- c(1, 2, 2, 0)

trips <- cbind.data.frame(Name, Gender)
trips[, gender = ifelse(gender == 0, "Unkown", gender == 1, "Male", gender == 2, "Female" )]

but I get an error


回答1:


Once you have a data.table then it would be most efficient to use a vectorized translation strategy. The match function provides a method of creating a "selection vector" for a choosing a item from a set of character possibilities:

library(data.table)
setDT(trips)  # create a data.table from a dataframe

trips[ , Gender := c("Unknown", "male", "Female")[match(Gender, c(0,1,2))] ]
#-------------------
> trips
     Name  Gender
1:   John    male
2:   Tina  Female
3:   Dave  Female
4: Casper Unknown

For this specific case, a simpler solution could be (ht to @Chinsoon):

trips[, gender := c("Unknown", "Male", "Female")[gender + 1L] ]



回答2:


You can do it this way

library(data.table)
trips <- data.table(Name=c('John','Tina','Dave','Casper'),gender=c(1,2,1,0))
trips[,gender:= ifelse(gender == 0 , "Unknown", 
                        ifelse(gender == 1 ,  "Male", 
                                              "Female" ))]

Two problems in your code:

  • You need to use := which is the assigning symbol for a column in data.table
  • You can only have one alternative with ifelse, so you need another ifelse for the third case: if gender is not 0 then you need to test if gender is 1 to separate the Male and Female cases



回答3:


Put the rules in a table and do an update join:

gmap = data.table(old = 0:2, new = c("Unknown", "Male", "Female"))
trips[, Gender := gmap[copy(.SD), on=.(old = Gender), x.new]]

     Name  Gender
1:   John    Male
2:   Tina  Female
3:   Dave  Female
4: Casper Unknown


来源:https://stackoverflow.com/questions/58988145/recode-a-variable-using-data-table-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!