add-semi-colons
add-semi-colons

Reputation: 18810

R Merge Dataframe

I have multiple data frames but they all have same column headers what I want to do is just append them one after the other to make one big dataframe. What is the best way to do this.

I used the following: but not sure this is the right way to do:

append(x, y)

I also tried rbind but ended up with following error as I expected since these data frames have different length in size:

Error in names(value[[jj]])[ri] <- nm : 
  'names' attribute [2066] must be the same length as the vector [9]
In addition: Warning message:
In names(value[[jj]])[ri] <- nm :
  number of items to replace is not a multiple of replacement length

Here is an example:

> x
  user_id rev_page   rev_id rev_parent_id page_ns rev_length delta deleted rev_curr_minor           timestamp edit_count lang_level
1  705834  1048946 32026867      30924739       0       2959    82       0              0 2005-12-19 23:21:24          0          N
2  705834  1048946 32028621      32026867       0       3085   126       0              0 2005-12-19 23:36:33          1          N
3  705834    21402 32032828      32027632       0      14395   470       0              0 2005-12-20 00:10:06          2          N
4  705834    21402 32033273      32032828       0      14400     5       0              0 2005-12-20 00:13:50          3          N
5  705834   321528 32065506      29330826       0       3440   201       0              0 2005-12-20 05:13:45          4          N
6  705834   321528 32066086      32065506       0       3525    85       0              0 2005-12-20 05:21:25          5          N
> y
  user_id rev_page   rev_id rev_parent_id page_ns rev_length delta deleted rev_curr_minor           timestamp edit_count lang_level
1  892755  2585151 38515111      24830760       0       2018  1213       0              0 2006-02-06 21:32:26          0          1
2  892755  3807359 63798071      63471532       1      14047  5293       0              0 2006-07-14 15:46:56          1          1
3  892755  3807359 63798564      63798071       1      14146    99       0              0 2006-07-14 15:49:52          2          1
4  892755  2976438 63825832      44108751       0       9609   -60       0              0 2006-07-14 18:38:41          3          1
5  892755  3807359 63891769      63855569       1      15579  1005       0              0 2006-07-15 02:28:59          4          1
6  892755  3807359 67487579      63891769       1      15745   166       0              0 2006-08-03 18:52:01          5          1
> class(x)
[1] "data.frame"
> z <- rbind(x,y)
Error in names(value[[jj]])[ri] <- nm : 
  'names' attribute [12] must be the same length as the vector [9]
In addition: Warning messages:
1: In names(value[[jj]])[ri] <- nm :
  number of items to replace is not a multiple of replacement length
2: In names(value[[jj]])[ri] <- nm :
  number of items to replace is not a multiple of replacement length
> rbind(x,y)

Upvotes: 1

Views: 740

Answers (1)

Thomas
Thomas

Reputation: 44525

If columns are in the same order (following @MatthewPlourde's comment), just rbind(df1,df2,df3,...).

Upvotes: 1

Related Questions