title: R - reduce with merge and more than 2 suffixes (or: how to merge multiple dataframes and keep track of columns)
body: <p>I'm trying to merge 4 dataframes based on 2 columns, but keep track of which dataframe a column originated from. I'm running into an issue at tracking the columns.</p>
  
  <p>(see end of post of dput(dfs))</p>
  
  <pre><code>#df example (df1)
  Name    Color    Freq
  banana  yellow   3
  apple   red      1
  apple   green    4
  plum    purple   8
  
  
  #create list of dataframes
  list.df &lt;- list(df1, df2, df3, df4)
  
  #merge dfs on column "Name" and "Color"
  combo.df &lt;- Reduce(function(x,y) merge(x,y, by = c("Name", "Color"), all = TRUE, accumulate=FALSE, suffixes = c(".df1", ".df2", ".df3", ".df4")), list.df)
  </code></pre>
  
  <p>This gives the following warning: </p>
  
  <blockquote>
    <p>Warning message:
    In merge.data.frame(x, y, by = c("Name", "Color"), all = TRUE,  :
      column names ‘Freq.df1’, ‘Freq.df2’ are duplicated in the result</p>
  </blockquote>
  
  <p>and outputs this dataframe:</p>
  
  <pre><code>#combo df example
  Name    Color    Freq.df1   Freq.df2  Freq.df1  Freq.df2
  banana  yellow   3          3         7         NA
  apple   red      1          2         9         1
  apple   green    4          NA        8         2
  plum    purple   8          1         NA        6
  </code></pre>
  
  <p><code>df1</code> and <code>df2</code> are only repeated in name. The values populating the third and fourth column of <code>combo</code> are actually from <code>df3</code> and <code>df4</code> respectively.</p>
  
  <p>What I would really like is: </p>
  
  <pre><code>Name    Color    Freq.df1   Freq.df2  Freq.df3  Freq.df4
  banana  yellow   3          3         7         NA
  apple   red      1          2         9         1
  apple   green    4          NA        8         2
  plum    purple   8          1         NA        6
  </code></pre>
  
  <p>How can I achieve this? I know the <code>merge(..., suffixes)</code> function can only handle a character vector of 2, but I don't know what the work around should be.  Thanks!</p>
  
  <pre><code>df1 &lt;- 
  structure(list(Name = structure(c(2L, 1L, 1L, 3L), .Label = c("apple", 
  "banana", "plum"), class = "factor"), Color = structure(c(4L, 
  3L, 1L, 2L), .Label = c("green", "purple", "red", "yellow"), class = "factor"), 
      Freq = c(3, 1, 4, 8)), .Names = c("Name", "Color", "Freq"
  ), row.names = c(NA, -4L), class = "data.frame")
  
  df2 &lt;-
  structure(list(Name = structure(c(2L, 1L, 3L), .Label = c("apple", 
  "banana", "plum"), class = "factor"), Color = structure(c(3L, 
  2L, 1L), .Label = c("purple", "red", "yellow"), class = "factor"), 
      Freq = c(3, 2, 1)), .Names = c("Name", "Color", "Freq"), row.names = c(NA, 
  -3L), class = "data.frame")
  
  df3 &lt;-
  structure(list(Name = structure(c(2L, 1L, 1L), .Label = c("apple", 
  "banana"), class = "factor"), Color = structure(c(3L, 2L, 1L), .Label = c("green", 
  "red", "yellow"), class = "factor"), Freq = c(7, 9, 8)), .Names = c("Name", 
  "Color", "Freq"), row.names = c(NA, -3L), class = "data.frame")
  
  df4 &lt;-
  structure(list(Name = structure(c(1L, 1L, 2L), .Label = c("apple", 
  "plum"), class = "factor"), Color = structure(c(3L, 1L, 2L), .Label = c("green", 
  "purple", "red"), class = "factor"), Freq = c(1, 2, 6)), .Names = c("Name", 
  "Color", "Freq"), row.names = c(NA, -3L), class = "data.frame")
  </code></pre>