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!