title: Merging multiple tables in R
body: <p>EDIT: I understand the question of merging multiple data frames has been asked previously, but I was stuck on how to merge multiple tables <em>without</em> converting to data frames first, to be more concise. If you want to see how to merge multiple data frames, please see the great answer <a href="https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list">here</a> (also linked below).</p>
  
  <p>So I have a way to merge multiple data frames in R already, but I am hoping someone can help me find a way that's more elegant. Here is an example of code that I have that works. Given that df1, df2, and df3 are data frames with identical columns (including column name 'class') but differing numbers of rows, I can do:</p>
  
  <pre><code>table1 &lt;- table(df1$class)
  table2 &lt;- table(df2$class)
  table3 &lt;- table(df3$class)
  </code></pre>
  
  <p>and as given by <a href="https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list">this</a> answer, I can then merge them:</p>
  
  <pre><code>merged.table &lt;- Reduce(function(...) merge(..., all=T), list(table1, table2, table3))
  </code></pre>
  
  <p>My problem is that the merge is incorrect because the objects table1, table2, and table3 have identical identification names and the merged.table ends up combining the data to one column.</p>
  
  <p>My workaround is to convert the tables to data frames, as such:</p>
  
  <pre><code>table1 &lt;- as.data.frame(table(df1$class))
  colnames(table1) &lt;- c("ID","counts1")
  table2 &lt;- as.data.frame(table(df2$class))
  colnames(table2) &lt;- c("ID","counts2")
  table3 &lt;- as.data.frame(table(df3$class))
  colnames(table3) &lt;- c("ID","counts3")
  </code></pre>
  
  <p>Then the merge works just fine. But let me tell you, that gets really clunky and tedious after a while, and I need to do this kind of thing a lot.</p>
  
  <p>Is there any way to achieve the same goal without converting tables to data frames and assigning column names?</p>
  
  <p>Here is an example of what the data frame looks like, truncated for simplicity:</p>
  
  <pre><code>transcript &lt;- rep(c("a","b","c","d","e","f"))
  family &lt;- rep(c("L1","L2","ERV"),2)
  class &lt;- rep(c("LINE","LINE","LTR"),2)
  
  df1 &lt;- data.frame(transcript, family, class)
  
  transcript  family  class
  a            L1     LINE
  b            L2     LINE
  c            ERV    LTR
  d            L1     LINE
  e            L2     LINE
  f            ERV    LTR
  </code></pre>