使用开发版本(v> = 1.9.5),我们可以通过在setDT中指定key(由@Arun指出)来使其更短setDT(dt1, key = "A")[dt2, H := i.H] 编辑2015年7月24日您现在可以使用新的on参数运行二进制联接,而无需设置键setDT(dt1)[dt2, H := i.H, on = c(A = "E")]Let's say I have two data tables (dt1 and dt2), and I want to get dt3 using data tables. A,B,C,E,F,G,H are column names. dt1 key is column A, and dt2 key is column E. Data tables have different number of rows. I want to keep all the columns from DT1, and add only one column (H) from DT2 to the joined data table. Eventually, I will store this as DT1 (though I showed it as dt3 below).How can I achieve it with data tables? I have an ugly solution with merge + data frames.dt1A B C1 4 72 5 83 6 92 20 21dt2E F G H1 10 13 163 12 15 182 11 14 17dt3A B C H1 4 7 162 5 8 173 6 9 182 20 21 17 解决方案 In order to perform a left join to df1 and add H column from df2, you can combine binary join with the update by reference operator (:=)setkey(setDT(dt1), A)dt1[dt2, H := i.H]See here and here for detailed explanation on how it worksWith the devel version (v >= 1.9.5) we could make it even shorter by specifying the key within setDT (as pointed by @Arun)setDT(dt1, key = "A")[dt2, H := i.H]Edit 24/7/2015You can now run a binary join using the new on parameter without setting keyssetDT(dt1)[dt2, H := i.H, on = c(A = "E")] 这篇关于连接两个数据表,仅使用第二个dt中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 05:35