-1

I have two left_join operations:

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.

How can combine these two operations into a single operation?

Omniswitcher
  • 368
  • 3
  • 13
Saïd Maanan
  • 511
  • 4
  • 14

1 Answers1

1

You could pipe (%>%) these together:

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():

list(unnested,
     rename(X3g_data[,c(1:6)], Country = CountryName),
     new_data) %>% 
  reduce(left_join)
Dan Adams
  • 4,971
  • 9
  • 28