替代data.table中的plyr :: mapvalues

时间:2019-11-11 21:26:23

标签: r dataframe dplyr data.table plyr

我正在寻找plyr::mapvaluesdata.table的可读替代形式。

例如,在plyr::mapvalues中,如果我想将carb中的mtcars的值更改为type1, 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")))

要在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"]

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

5 个答案:

答案 0 :(得分: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
## ...

答案 1 :(得分:5)

令我惊讶的是,没有人建议将其作为联接来进行:

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

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

答案 2 :(得分:3)

使用match

dt[, carb := rep(paste0("Type", 1:3), each = 2)[match(carb, c("1","2","3","4","6","8"))]]
#    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
#10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4 Type2
#...

答案 3 :(得分:2)

了解OP正在寻求可读性,但它是主观的,因此为ref的公共Wiki增加了一些时间:

计时代码:

library(data.table)
set.seed(0L)
from = c("1", "2", "3", "4", "6", "8")
to = c("type1", "type1", "type2", "type2", "type3", "type3")
nr <- 1e7
DT <- data.table(carb=sample(from, nr, TRUE))
DT_match <- copy(DT)
DT_factor <- copy(DT)
DT_updjoin <- copy(DT)
DT_updjoin_setidx <- copy(DT)
DT_plyr <- copy(DT)

mtd_updjoin <- function() {
    DT_updjoin[.(carb=from, type=to), on="carb", type := i.type]
}

mtd_updjoin_setidx <- function() {
    setindex(DT_updjoin_setidx, carb)
    d <- data.table(carb=from, type=to, key="carb")
    DT_updjoin_setidx[d, on=.(carb), type := i.type]
}

mtd_match <- function() {
    DT_match[, carb := to[match(carb, from)]]
}

mtd_factor <- function() {
    DT_factor[, carb := factor(carb, levels=from, labels=to)]
}

mtd_plyr <- function() {
  DT_plyr[, carb2 := plyr::mapvalues(carb, from = c("1", "2", "3", "4", "6", "8"), to = c("type1", "type1", "type2", "type2", "type3", "type3"))]
}


bench::mark(mtd_factor(), mtd_match(), mtd_updjoin(), mtd_updjoin_setidx(), mtd_plyr(), check=FALSE)

时间:

# A tibble: 5 x 13
  expression                min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result      memory    time   gc     
  <bch:expr>           <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>      <list>    <list> <list> 
1 mtd_factor()         542.02ms 542.02ms     1.84      305MB    1.84      1     1   542.02ms <df[,1] [1~ <df[,3] ~ <bch:~ <tibbl~
2 mtd_match()           256.3ms 291.68ms     3.43      191MB    1.71      2     1   583.36ms <df[,1] [1~ <df[,3] ~ <bch:~ <tibbl~
3 mtd_updjoin()            1.4s     1.4s     0.714     382MB    0.714     1     1       1.4s <df[,2] [1~ <df[,3] ~ <bch:~ <tibbl~
4 mtd_updjoin_setidx() 886.78ms 886.78ms     1.13      420MB    0         1     0   886.78ms <df[,2] [1~ <df[,3] ~ <bch:~ <tibbl~
5 mtd_plyr()              1.15s    1.15s     0.866     815MB    0.866     1     1      1.15s <df[,2] [1~ <df[,3] ~ <bch:~ <tibbl~

答案 4 :(得分:1)

[更新]我不知道为什么有些人不建议删除which() ,但这不是您要想到的:

library(tidyverse)
library(data.table)

dt<-as.data.table(mtcars)
dt[,carb:=as.character(carb)]

dt[carb %in% c("1", "2"),carb:="type1"]
dt[carb %in% c("3", "4"),carb:="type2"]
dt[carb %in% c("6", "8"),carb:="type3"]