我想通过两个关键列加入两个datatable
:
df1=data.table(key1=c(1.2,1.1,1.9,4.6,2.3,3.6,0.9,2.8),key2=c("A","B","A","B","A","B","A","B"))
df2=data.table(key1=c(1,1,2,3),key2=c("A","B","B","B"),value=c("A","B","D","C"))
这是所需的输出:
key1 key2 value
1: 0.9 A A
2: 1.1 A A
3: 1.2 B B
4: 1.9 A NA
5: 2.3 A NA
6: 2.8 B C
7: 3.6 B NA
8: 4.6 B NA
可以使用以下内容匹配具有最接近值的数字键:
# Join with closest value of Numeric Key, works fine
df1=data.table(key1=c(1.2,1.1,1.9,4.6,2.3,3.6,0.9,2,8),key2=c("B","B","A","A"))
df2=data.table(key1=c(1,1,2,3),key2=c("A","B","B","B"),value=c("A","B","D","C"))
setkey(df1,key1)
setkey(df2,key1)
df2[df1,roll="nearest"]
此外,如果数字键具有精确匹配,则问题可以再次解决:
df1=data.table(key1=c(1,1,2,5,2,4,1,3),key2=c("A","B","A","B","A","B","A","B"))
df2=data.table(key1=c(1,1,2,3),key2=c("A","B","B","B"),value=c("A","B","D","C"))
setkey(df1,key1,key2)
setkey(df2,key1,key2)
df2[df1]
解决方案不一定是data.table
。
由于