37

I have a dataframe:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
            df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

Can I convert it to a single dataframe of the form:

data.frame(x = c('a', 'b', 'c', 'd', 'e', 'f'), y= c(1,2,3,4,5,6))

?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • 3
    Related : http://stackoverflow.com/questions/4748537/merging-several-data-frames-into-a-single-expanded-frame and http://stackoverflow.com/questions/2392915/recombining-a-list-of-data-frames-into-a-single-data-frame and http://stackoverflow.com/questions/2851327/r-converting-a-list-of-data-frames-into-one-data-frame ... Please do a search before you ask a question: If I can remember at least 2 questions with appx the same title, you didn't do any effort, did you? – Joris Meys Mar 04 '11 at 09:25
  • 3
    @Joris, thanks for pointing those out, but your memory functions better than my queries + the SO search engine: none of these answers showed up in the 89 results for "[r] +list +dataframe" or the first 100 records of my searches, including "[r] convert list to dataframe" and "[r] combine list to dataframe". I would appreciate suggestions for searches that would have found these results. – David LeBauer Mar 04 '11 at 14:41
  • with [r] combine data frame you get at least one of the related questions on the first page. With [r] combine list data frame you get it as a second answer. I agree that when you write dataframe as one word, it gets tricky. Sorry for the harsh tone of my earlier comment. – Joris Meys Mar 04 '11 at 14:58
  • 1
    on a sidenote: it's better to keep these words seperate, as quite some people write them as data-frame (recognized as 2 words by the search engine). – Joris Meys Mar 04 '11 at 14:59

4 Answers4

80

do.call("rbind", foo) should do the trick.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • 6
    A much more efficient solution is `rbindlist` from data.table. Check [this post](https://stackoverflow.com/questions/15673550/why-is-rbindlist-better-than-rbind) for details and benchmarking. – Henk Oct 21 '16 at 07:42
  • 7
    `rbind_list` is now deprecated and you should use `dplyr::bind_rows` instead. – Arthur Spoon Jul 06 '18 at 11:41
7

with plyr:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))

library(plyr)
ldply(foo)[,-1]
  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6
Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
4

There are several problems with your code.

The first is that the assignment statement in the list doesn't work. This needs to be fixed by, for example:

foo <- list(
        df1 = data.frame(x=c('a', 'b', 'c'), y = c(1,2,3)), 
        df2 = data.frame(x=c('d', 'e', 'f'), y = c(4,5,6))
)

You can then use rbind() to combine the data frames:

rbind(foo$df1, foo$df2)

  x y
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
6 f 6

But this poses more questions. For example, why do you combine the data frames in a list in the first place. The second is whether you really need to use data frames rather than vectors. Finally, I generally try to avoid rbind() and rather use merge() when combining data frames in this way.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • thanks for pointing out the error in my code. I have fixed it. Thanks also for your answer - i didn't realize that it was so simple. The actual data frames are much bigger, and my workflow makes frequent use of lapply, here I just want to bind the dataframes so that `xtable` will mae a single table., – David LeBauer Mar 04 '11 at 01:02
  • @David, that makes complete sense now. If you often use lapply, you may also wish to have a look at the functions in plyr, which provide a general framework for this type of problem. Then the solution simply becomes ldply(foo, rbind) – Andrie Mar 04 '11 at 06:38
1

How about merge(foo[[1]], foo[[2]], all = TRUE)

Chase
  • 67,710
  • 18
  • 144
  • 161