title: How to merge multiple data.frames and sum and average columns at the same time in R
body: <p>I have over 20 twenty data.frames with the same columns but differing amount of rows. My goal is to merge the data.frames by the column "Name" (which is a list of five names) and while merging I would like the rows with the same name to sum column A, sum column B, and get the mean of column C.</p>
  
  <p>Here is what I am currently doing. </p>
  
  <p>First I will just merge 2 data.frames at a time.</p>
  
  <pre><code>DF &lt;- merge(x=abc, y=def, by = "Name", all = T)
  </code></pre>
  
  <p>Merged DF will look like such</p>
  
  <pre><code>Name                              A.x    B.x   C.x   A.y   B.y  C.y
  name1,name2,name3,name4,name5      11     24     7    NA    NA   NA
  name1,name3,name4,name6,name7       4      8    12     3     4    7
  name1,name2,name5,name6,name7      12      4     5    NA    NA   NA
  name3,name4,name5,name6,name7      NA     NA    NA    15     3   28
  </code></pre>
  
  <p>I will then add these <code>ifelse</code> statements to deal with the <code>NAs</code> and non unique rows. For the non unique rows it will add for A add for B and for C it will get an average. </p>
  
  <pre><code>DF$A &lt;- ifelse(is.na(DF$A.x), DF$A.y,
      ifelse(is.na(DF$A.y), DF$A.x,
          ifelse((!is.na(DF$A.x)) &amp; (!is.na(DF$A.y)), DF$A.x + DF$A.y, 1)))
  DF$B &lt;- ifelse(is.na(DF$B.x), DF$B.y, 
      ifelse(is.na(DF$B.y), DF$B.x,
          ifelse((!is.na(DF$B.x)) &amp; (!is.na(DF$B.y)), DF$B.x + DF$B.y, 1)))
  DF$C &lt;- ifelse(is.na(DF$C.x), DF$C.y, 
      ifelse(is.na(DF$C.y), DF$C.x,
          ifelse((!is.na(DF$C.x)) &amp; (!is.na(DF$C.y)), (DF$C.x  + DF$C.y)/2, 1)))
  </code></pre>
  
  <p>DF will now look like such</p>
  
  <pre><code>Name                            A.x  B.x  C.x  A.y  B.y  C.y   A   B   C   
  name1,name2,name3,name4,name5    11   24    7   NA   NA   NA  11  24   7
  name1,name3,name4,name6,name7     4    8   12    3    4    8   7  12  10
  name1,name2,name5,name6,name7    12    4    5   NA   NA   NA  12   4   5
  name3,name4,name5,name6,name7    NA   NA   NA   15    3   28  15   3  28
  </code></pre>
  
  <p>I then keep just the Name column and the last three columns</p>
  
  <pre><code>merge1 &lt;- DF[c(1,8,9,10)]
  </code></pre>
  
  <p>Then I do the same process for the next two data.frames and call it merge2. Then I will merge merge1 and merge 2.</p>
  
  <pre><code>total1 &lt;- merge(x = merge1, y = merge2, by = "Name", all = TRUE)
  </code></pre>
  
  <p>I will just continue to merge two data frames at a time then merge the Totals data.frames together as well two at a time. I get my end result that I want but it is a timely process and not very efficient. </p>
  
  <p>Another way I think I could do it is may be do a rbind with all the data.frames then if in the Name column any row has the same list of names as another row then make that one row, add column A, add column B and get the mean of column C. But I am not sure how to do that as well.</p>
  
  <p>Here is an example of what I would like with rind</p>
  
  <pre><code>Name                              A     B     C
  name1,name2,name3,name4,name5    11    24     7
  name1,name3,name4,name6,name7     4     8    12
  name1,name2,name5,name6,name7    12     4     5 
  name3,name4,name5,name6,name7    15     3    28 
  name1,name3,name4,name6,name7     3     4     8
  </code></pre>
  
  <p>The end result would look like such</p>
  
  <pre><code>Name                              A     B     C
  name1,name2,name3,name4,name5    11    24     7
  name1,name3,name4,name6,name7     7    12    10
  name1,name2,name5,name6,name7    12     4     5 
  name3,name4,name5,name6,name7    15     3    28 
  </code></pre>
  
  <p>Again, I am sure there are more efficient ways to complete what I want than what I am currently doing so any help would be greatly appreciated.</p>