I need to merge several data frames with duplicated column names. I am using dplyr::left_join, with purrr::reduce to iterate the function over the files. The standard behavior of dplyr::left_join is to add a suffix to duplicated col names, but that doesn't help to figure out which files the variable comes from unless you have the code handy. Below is a simplified example of my situation:
a <- tibble(id = 1:5, var=letters[1:5])
b <- tibble(var=letters[11:15], id=1:5)
c <- tibble(id = 1:5, var=letters[16:20], var2="a")
reduce(list(a,b,c), left_join, by="id")
# A tibble: 5 x 5
id var.x var.y var var2
<int> <chr> <chr> <chr> <chr>
1 1 a k p a
2 2 b l q a
3 3 c m r a
4 4 d n s a
5 5 e o t a
Is there a way to use the dataframe name as suffix, and to force the suffix to all the duplicated columns? What I'd like to have is:
# A tibble: 5 x 5
id var.a var.b var.c var2
<int> <chr> <chr> <chr> <chr>
1 1 a k p a
2 2 b l q a
3 3 c m r a
4 4 d n s a
5 5 e o t a
There a suffix option in left_join, but how can I use it within reduce?