我在理解如何处理关键数据表方面遇到了问题。我想要的是,如果值在另一个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
行1
和3
已更新,因为它们也在b
中找到,但值不同。