如果另一个data.table中的键匹配

时间:2017-07-21 09:29:16

标签: r key data.table

我在理解如何处理关键数据表方面遇到了问题。我想要的是,如果值在另一个data.table中匹配,则更新data.table,否则保持原样。

这是一个MRE:

a <- data.table(country = c("AD", "AD", "DE"), post =c("AD500", "AD500", "60381"), city = c("andora", 'Andorra la Vella', "Frankfurt" ))
setkeyv(a,c("country","post"))

b <- data.table(is = c("AD", "DE", "MX"), code = c("AD500", "60381", "45030"), "locality" = c("Andorra la Vella", "Frankfurt am Main", "Zapopan"))
setkeyv(b,c('is','code'))

没有奏效的事情:

a[b, `:=` (city = i.locality ), on = .(country, post)]
#Error in `[.data.table`(a, b, `:=`(city = i.locality), on = .(country,  : 
#Column(s) [country,post] not found in i
a[b, `:=` (city = i.locality ), on = .(is, code)]
#Error in `[.data.table`(a, b, `:=`(city = i.locality), on = .(is, code)) : 
#Column(s) [is,code] not found in x

预期结果:

   country  post              city
1:      AD AD500  Andorra la Vella
2:      AD AD500  Andorra la Vella
3:      DE 60381 Frankfurt am Main

13已更新,因为它们也在b中找到,但值不同。

0 个答案:

没有答案