I have the following dataframes
df1 <- tibble::as.tibble(list(a = c(1,2,3), d = c(10,11,12) ,id = c("a","b","c")))
df2 <- tibble::as.tibble(list(a = c(4,5,6), e = c(13,14,15) ,id = c("a","b","c")))
df3 <- tibble::as.tibble(list(a = c(7,8,9), f = c(16,17,18) ,id = c("a","b","c")))
I want to merge together these dataframes. Since the column name a occours in all of them I will have use the suffix argument while merging.
The desired result I am looking for is
| id | a.df1 | d | a.df2 | e | a.df3 | f |
|----|-------|----|-------|----|-------|----|
| a | 1 | 10 | 4 | 13 | 7 | 16 |
| b | 2 | 11 | 5 | 14 | 8 | 17 |
| c | 3 | 12 | 6 | 15 | 9 | 18 |
Below is the code I tried
test_list <- list(df1, df2, df3)
names(test_list) <- c("df1", "df2", "df3")
seq_along(temp) %>%
purrr::reduce(
~merge(
temp[[.x]],
temp[[.y]],
suffix = c(names(test_list[.x]), names(test_list[.y])))
However this results in an error stating
Error in temp[[.x]] : invalid subscript type 'list. Why am I not able to subset to a dataframe in the merge function
Also is there a better way to combine a list of multiple dataframes with same column names.