data.table中plyr::mapvalues的替代方案
我正在寻找data.table中plyr::mapvalues的替代方案,r,dataframe,dplyr,data.table,plyr,R,Dataframe,Dplyr,Data.table,Plyr,我正在寻找data.table中的plyr::mapvalues的可读替代品 例如,在plyr::mapvalues中,如果我想将mtcars中的carb的值更改为type1、type2、type3,我会这样做: library(tidyverse) mtcars %>% mutate(carb = plyr::mapvalues( carb, from = c("1", "2", "3", "4", "6", "8"), to = c("type1", "
data.table
中的plyr::mapvalues
的可读替代品
例如,在plyr::mapvalues
中,如果我想将mtcars
中的carb
的值更改为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"]
库(data.table)
dt使用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
#...
使用base::factor
是最简单的方法:
库(data.table)
setDT(mtcars)[,carb:=系数(carb,
级别=c(“1”、“2”、“3”,
"4", "6", "8"),
标签=c(“类型1”、“类型1”,
“类型2”、“类型2”,
“类型3”、“类型3”))][]
#>mpg气缸显示hp drat wt qsec与am齿轮carb
#>1:21.06160.01103.902.62016.46014类型2
#>2:21.06160.01103.902.87517.02014类型2
#>3:22.8 4 108.0 93 3.85 2.320 18.61 1 4类型1
#>4:21.4 6258.0 110 3.08 3.215 19.44 1 0 3类型1
#>5:18.7 8 360.0 175 3.15 3.440 17.02 0 3类型1
#>6:18.1 6225.0 105 2.76 3.460 20.22 1 0 3类型1
#>7:14.3 8 360.0 245 3.21 3.570 15.84 0 0 3类型2
#>8:24.4 146.7 62 3.69 3.190 20.00 1 0 4类型1
#>9:22.84140.8953.923.15022.90104类型1
## ...
我很惊讶没有人建议将此作为加入:
dt[
.(carb=c("1","2","3","4","6","8"), type=rep(c("type1","type2","type3"),each=2)),
on="carb",
type := i.type
]
它还可以很容易地扩展到多个变量的匹配。了解OP是在寻找可读性,但它是主观的,添加了一些时间,如comm wiki for ref:
定时代码:
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)
库(data.table)
种子集(0升)
from=c(“1”、“2”、“3”、“4”、“6”、“8”)
to=c(“类型1”、“类型1”、“类型2”、“类型2”、“类型3”、“类型3”)
nr[UPDATE]我不知道为什么有些人不建议删除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"]
库(tidyverse)
库(数据表)
我不确定我是否理解这个问题plyr::mapvalues
在data.table
中也以同样的方式工作dt[,carb2:=plyr::mapvalues(carb,from=c(“1”、“2”、“3”、“4”、“6”、“8”),to=c(“type1”、“type1”、“type2”、“type2”、“type3”、“type3”))]
你需要一个学习用的替代方案吗?@Ronaksha也许我应该在我的问题中澄清一下。我对data.table有些了解,所以我想留在data.table
环境中。但是,我没有想过在data.table
中使用plyr::mapvalues
,只有在使用管道时才这样。我不知道它在性能方面如何工作。虽然在data.table中使用plyr::mapvalues
速度较慢,但它仍然是一个易读且简单的答案。你能从评论中添加@Ronak Shah答案吗<代码>dt[,carb2:=plyr::mapvalues(carb,从=c(“1”、“2”、“3”、“4”、“6”、“8”)到=c(“1”、“1”、“2”、“2”、“3”、“3”))))
对不起,DJV,我没有安装plyr
。请随意编辑comm wikiiIt’这是一个老问题,但感谢您的回答:)。但是,请注意,我要寻找的是一个线性过程,而不是多步骤过程。
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"]