简体   繁体   English

将data.table的子集分配给另一个data.table的子集

[英]Assigning a subset of a data.table to a subset of another data.table

I have two data.tables: 我有两个data.tables:

library(data.table)
dt1 <- data.table(A=1:10, B=1, C=2, D=3)
dt2 <- data.table(A2=5:9, B2=4, C2=5, D2=6)

I want to assign elements of column B2 and C2 in dt2 to column B and C in dt1 where A = A2, such that I get a table that looks like this: 我想将dt2中的B2和C2列的元素分配给A = A2的dt1中的B和C列,这样我得到的表如下所示:

dt1
#      A B C D
#  1:  1 1 2 3
#  2:  2 1 2 3
#  3:  3 1 2 3
#  4:  4 1 2 3
#  5:  5 4 5 3
#  6:  6 4 5 3
#  7:  7 4 5 3
#  8:  8 4 5 3
#  9:  9 4 5 3
# 10: 10 1 2 3

I know I can assign one column at a time: 我知道我可以一次分配一列:

id1 <- which(dt1$A %in% dt2$A2)
id2 <- which(dt2$A2 %in% dt1$A)
dt1$B[id1] <- dt2$B2[id2]
dt1$C[id1] <- dt2$C2[id2]

however this seems like a lot of lines of code to do only very little, especially if I had many columns. 但是,这似乎只需要执行几行代码,却很少执行,尤其是当我有很多列时。

I was thinking something like 我在想类似

# Not working:
dt1[id1][,list(B,C)] <- dt2[id2][,list(B2,C2)]

would work, but I get an error message. 可以,但是我收到一条错误消息。

Is there a smarter, nicer way to do it? 有没有更聪明,更好的方法呢?

We can do a join on the 'A' with 'A2' and assign ( := ) the values of columns of interest to 'C' and 'D' 我们可以做一个连接on的与“A2”“A”和分配( := )的相关列的值,以“C”和“d”

library(data.table)#v1.9.7+
dt1[dt2,  c('C', 'D') := .(C2, D2),  on = .(A= A2)]
dt1
#     A B C D
# 1:  1 1 2 3
# 2:  2 1 2 3
# 3:  3 1 2 3
# 4:  4 1 2 3
# 5:  5 1 5 6
# 6:  6 1 5 6
# 7:  7 1 5 6
# 8:  8 1 5 6
# 9:  9 1 5 6
#10: 10 1 2 3

If there are many columns that we need to change, instead of typing out the columns, we get the values using mget and assign it to the corresponding column names in 'dt1' 如果需要更改许多列,则无需输入列,而是使用mget获取values并将其分配给“ dt1”中的相应列名称

dt1[dt2, names(dt1)[3:4] :=  mget(names(dt2)[3:4]), on = .(A = A2)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM