2

Here is an example of sample data frames

data.frame1

col1  col2 col3 col4 
1     2    3    4
2     3    4    4

data.frame2

col5  col6 col7 col8
 1    2    3     4
 3    3    5     9

data.frame3

col9  col10  col11  
1      2     3     

Desired output data.frame.append

col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 
1     2    3    4    NA  NA   NA   NA   NA   NA    NA
2     3    4    4    NA  NA   NA   NA   NA   NA    NA
NA   NA    NA   NA   1    2    3   4    NA   NA    NA
NA   NA    NA   NA   3    3    5   9    NA   NA    NA
NA   NA    NA   NA   NA   NA   NA  NA   1    2     3  

What is the most efficient way of doing this that scales to any number of data frames created on the fly.

Thank you in advance.

Edit:

Thank you all; and efficiency is of your interest, brief discussion can be found here

deepseefan
  • 3,701
  • 3
  • 18
  • 31

2 Answers2

6

The easier way is to use plyr::ldply:

ldply(list(df1, df2, df3))

  col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11
1    1    2    3    4   NA   NA   NA   NA   NA    NA    NA
2    2    3    4    4   NA   NA   NA   NA   NA    NA    NA
3   NA   NA   NA   NA    1    2    3    4   NA    NA    NA
4   NA   NA   NA   NA    3    3    5    9   NA    NA    NA
5   NA   NA   NA   NA   NA   NA   NA   NA    1     2     3
Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • 5
    Easier than...? – David Arenburg May 18 '17 at 08:05
  • Adam, I have added an image of input and output in pseudo format. Perhaps it gives you a clear picture of what I am after. I could see that your code has generated mean, however I was not able to integrate that into produce my final output data frame. – bonCodigo Jun 09 '17 at 04:42
5

We can do this with bind_rows from dplyr

library(dplyr)
bind_rows(df1, df2, df3)
#    col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11
#1    1    2    3    4   NA   NA   NA   NA   NA    NA    NA
#2    2    3    4    4   NA   NA   NA   NA   NA    NA    NA
#3   NA   NA   NA   NA    1    2    3    4   NA    NA    NA
#4   NA   NA   NA   NA    3    3    5    9   NA    NA    NA
#5   NA   NA   NA   NA   NA   NA   NA   NA    1     2     3
akrun
  • 874,273
  • 37
  • 540
  • 662