R data.table替换多列中的值
我对R和stackoverflow都是新手。我试图找出data.table,然后看着“R data.table替换另一个data.table中的值索引”,我想我理解了,但无法得到我想要的 我有两个数据帧-第一个是我感兴趣的数据,第二个是包含名称/ID的键,用于转换第一个数据帧中的ID。我想使用“key”data.table将表$id1和表$id2中的数字转换为“key”data.table中的“name”。以下是我迄今为止所做的工作:R data.table替换多列中的值,r,data.table,R,Data.table,我对R和stackoverflow都是新手。我试图找出data.table,然后看着“R data.table替换另一个data.table中的值索引”,我想我理解了,但无法得到我想要的 我有两个数据帧-第一个是我感兴趣的数据,第二个是包含名称/ID的键,用于转换第一个数据帧中的ID。我想使用“key”data.table将表$id1和表$id2中的数字转换为“key”data.table中的“name”。以下是我迄今为止所做的工作: table<-data.table("Sample"
table<-data.table("Sample" = sample(40:46, 6), "Conc1" = sample(100:106,6),
"id1" = as.character(sample(1:6, 6)), "Conc2" = sample(200:206,6),
"id2" = as.character(sample(1:6, 6)))
key<-data.table("Name" = c("Sally", "John", "Roger", "Bob", "Kelsey", "Molly"),
"id1" = as.character(1:6))
setkey(table, id1)
setkey(key, id1)
table[key, `:=`(id1 = i.Name)]
表执行此操作的另一种方法:
dt <- merge(table,key,by.x = c('id1'),by.y = c('id1'),sort=F)
table <- merge(dt,key,by.x = c('id2'),by.y = c('id1'),sort=F)
table[,id1 := Name.x]
table[,id2 := Name.y]
table[,Name.x := NULL]
table[,Name.y := NULL]
## id2 id1 Sample Conc1 Conc2
##1: Bob Bob 41 101 200
##2: Kelsey John 46 100 203
##3: Roger Molly 43 102 206
##4: Sally Kelsey 42 105 201
##5: John Roger 44 106 202
##6: Molly Sally 45 104 204
dt在data.table
中,无需设置键即可进行连接。您可以在on=
参数中指定联接列
library(data.table) ## v1.9.6 +
## update id1 based on Name
table[ key, on = c("id1"), nomatch = 0, id1 := i.Name]
## here the id1 column is getting updated to i.Name
## (the 'i.' is the prefix given to columns on the 'right' side of the join).
## update id2 based on Name
table[ key, on = c(id2 = "id1"), nomatch = 0, id2 := i.Name]
table
# Sample Conc1 id1 Conc2 id2
#1: 40 100 John 201 John
#2: 43 101 Kelsey 206 Kelsey
#3: 45 103 Molly 205 Roger
#4: 42 102 Roger 204 Bob
#5: 44 104 Sally 200 Molly
#6: 41 105 Bob 202 Sally
从data.table v1.9.6开始
可以使用on=
参数在不同的列名上进行连接
library(data.table) ## v1.9.6 +
## update id1 based on Name
table[ key, on = c("id1"), nomatch = 0, id1 := i.Name]
## here the id1 column is getting updated to i.Name
## (the 'i.' is the prefix given to columns on the 'right' side of the join).
## update id2 based on Name
table[ key, on = c(id2 = "id1"), nomatch = 0, id2 := i.Name]
table
# Sample Conc1 id1 Conc2 id2
#1: 40 100 John 201 John
#2: 43 101 Kelsey 206 Kelsey
#3: 45 103 Molly 205 Roger
#4: 42 102 Roger 204 Bob
#5: 44 104 Sally 200 Molly
#6: 41 105 Bob 202 Sally
数据
## setting seed because we are sampling
set.seed(1234)
table<-data.table("Sample" = sample(40:46, 6), "Conc1" = sample(100:106,6),
"id1" = as.character(sample(1:6, 6)), "Conc2" = sample(200:206,6),
"id2" = as.character(sample(1:6, 6)))
key<-data.table("Name" = c("Sally", "John", "Roger", "Bob", "Kelsey", "Molly"),
"id1" = as.character(1:6))
###正在播种,因为我们正在采样
种子集(1234)
桌子