Skip to content

Adding data.frames together

6 messages · John Sweval, Tom Blackwell, Tony Plate +2 more

#
John  -

The function  rbind()  operates on pairs of data frames, and
(somewhat arcane and definitely NOT for beginning users)

   do.call("rbind", list(df1, df2, df3, df4, df5)))

will combine any number (in this case 5) of data frames.

As of February 2003, the  do.call()  approach did not deal
gracefully with columns in a data frame which had been
converted to factors (happens by default in  read.table()).
In my own code I had to go through some gyrations to protect
against that.

For futher information on the  do.call()  approach, search
the r-help archives for "do.call" AND "rbind".  In particular,
there was a thread involving jerosenb and rpeng titled
"[R] quotes within quotes" with one email dated Wed, 9 Apr 2003.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Tue, 9 Mar 2004, John Sweval wrote:

            
#
Correction to my reply below:

If my memory serves correctly, one year and many projects later,
I wanted to keep character data as character, not factors,
throughout.  The gyrations were to maintain this despite
using  rbind(),  not because  rbind()  behaved badly with
factors.  Please excuse my foggy memory.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Tue, 9 Mar 2004, Tom Blackwell wrote:

            
#
Actually, rbind() operates on any number of arguments, as stated in the 
documentation ?rbind.  (If it only operated on pairs of arguments, the 
do.call() approach wouldn't get around that anyway.  The do.call() approach 
can be very useful when one has a list of data frames to be rbind'ed 
together, but it's not necessary when the data frames are in separate objects.)

Also, rbind() seems to cope with factor columns now - at least in the 
simple example below.  Are there other examples where it does not cope?

Here are some examples:

 > df1 <- data.frame(a=1:3,b=101:103)
 > df2 <- data.frame(a=4:5,b=104:105)
 > df3 <- data.frame(a=6,b=106)
 > rbind(df1, df2, df3)
    a   b
1  1 101
2  2 102
3  3 103
11 4 104
21 5 105
12 6 106
 > df1 <- data.frame(a=1:3,b=letters[1:3])
 > df2 <- data.frame(a=4:5,b=letters[4:5])
 > df <- rbind(df1, df2)
 > sapply(df, class)
         a         b
"integer"  "factor"
 > df
    a b
1  1 a
2  2 b
3  3 c
11 4 d
21 5 e
 >

-- Tony Plate
At Tuesday 05:00 PM 3/9/2004, Tom Blackwell wrote:
#
On 9 Mar 2004 at 14:41, John Sweval wrote:
If you have the dataframes as components of a list dfs, maybe you can 
do something like

do.call("cbind", dfs)

Kjetil Halvorsen
#
check rbind
Joris
John Sweval wrote: