Alternative to plyr::mapvalues in data.table

DJV*_*DJV 1 r plyr dataframe dplyr data.table

I am looking for a readable alternative to plyr::mapvalues in data.table.

例如,在中plyr::mapvalues,如果我想将carbin 的值更改mtcarstype1, type2, type3,则可以执行以下操作:

library(tidyverse)

mtcars %>% 
  mutate(carb = plyr::mapvalues(
    carb,
    from = c("1", "2", "3", "4", "6", "8"),
    to = c("type1", "type1", "type2", "type2", "type3", "type3")))
Run Code Online (Sandbox Code Playgroud)

为了获得相同的效果data.table,我会这样做,但这似乎不是常规方法:

library(data.table)

dt <- data.table(mtcars)
dt$carb <- as.character(dt$carb)

dt[which(carb %in% c("1", "2")), 
   carb := "type1"]

dt[which(carb %in% c("3", "4")), 
   carb := "type2"]

dt[which(carb %in% c("6", "8")), 
   carb := "type3"]
Run Code Online (Sandbox Code Playgroud)

是否可以在一个条件(dt[...])中更改所有值?

M--*_*M-- 5

使用base::factor是最简单的方法:

library(data.table)

setDT(mtcars)[, carb := factor(carb, 
                                levels = c("1", "2", "3", 
                                           "4", "6", "8"),
                                labels = c("type1", "type1",
                                           "type2", "type2", 
                                           "type3", "type3"))][]

#>      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#>  1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4 type2
#>  2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4 type2
#>  3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4 type1
#>  4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3 type1
#>  5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3 type1
#>  6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3 type1
#>  7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3 type2
#>  8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4 type1
#>  9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4 type1
## ...
Run Code Online (Sandbox Code Playgroud)


the*_*ail 5

令我惊讶的是,没有人建议仅将此作为联接:

dt[
  .(carb=c("1","2","3","4","6","8"), type=rep(c("type1","type2","type3"),each=2)),
  on="carb",
  type := i.type
]
Run Code Online (Sandbox Code Playgroud)

它也很容易扩展,然后匹配多个变量。