0

In R, How can I iteratively left_join() a list of join tables to an initial table by a different column per join table using purrr::reduce2 (or some other method)?

I've tried the following, but I get an error, "Error in auto_copy(x, y, copy = copy) : argument "x" is missing, with no default"

# Define the initial table T0
T0 <- tibble(
  col1 = c(1, 2, 3, 4),
  col2 = c("A", "B", "C", "D"),
  col3 = c("X", "Y", "Z", "W")
)

# Define tables T1, T2, and T3
T1 <- tibble(
  col1 = 1:4,
  var1 = 5:8
)

T2 <- tibble(
  col2 = c("A", "B", "C", "D"),
  var2 = c("AA", "BB", "CC", "DD")
)

T3 <- tibble(
  col3 = c("X", "Y", "Z", "W"),
  var3 = c("XX", "YY", "ZZ", "WW")
)

# Define list of join tables
join_tables <- list(
  T1,
  T2,
  T3
)

# Define list of respective join columns
join_cols <- list(
  "col1",
  "col2",
  "col3"
)

# Perform iterative left joins using reduce2
result <- reduce2(
  join_tables, join_cols, 
  ~ left_join(y = .x, by = .y), 
  .init = T0
)

Any help here would be greatly appreciated, thanks R community!

kenmore17
  • 41
  • 3
  • 2
    Or `join_tables %>% purrr::reduce(left_join)` with `T0` as the first element in `join_tables`? – Jon Spring Jun 13 '23 at 00:30
  • Thanks @JonSpring! I didn't realise it could be that simple! Even most other similar examples I found where all the joins were operating on a single column had the join-by column explicitly called out in one of the arguments. – kenmore17 Jun 13 '23 at 01:09
  • Does this answer your question? [Simultaneously merge multiple data.frames in a list](https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list) – nniloc Jun 13 '23 at 03:27
  • In this case it works because each added column relates unambiguously with a key column from `T0` and doesn't match the added column from any other secondary tables. – Jon Spring Jun 13 '23 at 04:38
  • @nniloc, thanks, it does in some sense, but I was specifically looking for a solution where the join column is not constant across tables, so although the present answer is simpler than I expected, prior examples I've seen didn't explicitly address my concern. – kenmore17 Jun 15 '23 at 00:55

1 Answers1

0

Add T0 as the first element in join_tables, get rid of join_cols, and use reduce()

# Define list of join tables
join_tables <- list(
  T0,
  T1,
  T2,
  T3
)

# Perform iterative left joins using reduce
join_tables %>% purrr::reduce(left_join)
kenmore17
  • 41
  • 3