简体   繁体   English

具有不同列名的多个左连接

[英]Multiple left join with different column names

I have two left_join operations:我有两个left_join操作:

unnested = left_join(unnested, X3g_data[,c(1:6)], by = c("Country" = "CountryName"), all.x = TRUE)

unnested = left_join(unnested, new_data, by = "Country", all.x = TRUE)

I want to combine them into one left_join command, I found this answer , but it assumes that the column names are the same, which is not the case for me.我想将它们组合成一个left_join命令,我找到了这个答案,但它假设列名是相同的,这对我来说不是这样。

How can combine these two operations into a single operation?如何将这两个操作合并为一个操作?

You could pipe ( %>% ) these together:您可以将 pipe ( %>% ) 这些放在一起:

library(tidyverse)

unnested %>% 
  left_join(X3g_data[,c(1:6)], 
            by = c("Country" = "CountryName")) %>% 
  left_join(new_data, 
            by = "Country")

Another option is to adjust the input data so the merging columns are the same for all and then use purrr::reduce() :另一种选择是调整输入数据,以便合并列对所有列都相同,然后使用purrr::reduce()

list(unnested,
     rename(X3g_data[,c(1:6)], Country = CountryName),
     new_data) %>% 
  reduce(left_join)

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

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