3

I have a list that contains 0-3 id numbers like this:

[[1]]
[1] 2125233 <NA>    <NA>

[[2]]
[1] 2009281 2380145 2931851

[[3]]
[1] 1517163 2836702 2506119

How do I convert this to a data frame where the element number becomes the id and the contents of the element becomes a string/factor like this:

  id                   contents
   1             2125233 <NA>    <NA>
   2             2009281 2380145 2931851
   3             1517163 2836702 2506119
  ...                 .....

I've tried looking at unlist but it just throws everything into one long vector.

Any suggestions?

screechOwl
  • 27,310
  • 61
  • 158
  • 267
  • 1
    Could you please start using `dput` as a means to show us the data object you're working with? Thanks. – GSee Sep 25 '12 at 16:24
  • 1
    Have a look at this question and answer for the most efficient methods http://stackoverflow.com/questions/5942760/most-efficient-list-to-data-frame-method – mnel Sep 25 '12 at 22:38
  • @mnel That question is about list of _columns_ to data.frame. This one's about list of _rows_ to data.frame; i.e., `rbindlist` (when that is relaxed to accept non `data.table`, [FR#2264](https://r-forge.r-project.org/tracker/index.php?func=detail&aid=2264&group_id=240&atid=978)). – Matt Dowle Sep 26 '12 at 10:31
  • 1
    @MatthewDowle -- very true. I meant to refer to http://stackoverflow.com/questions/9728407/trouble-converting-long-list-of-data-frames-1-million-to-single-data-frame-us/12290824#12290824 – mnel Sep 26 '12 at 11:10

4 Answers4

7
List <- list(c(123, NA, NA), c(1223, 4546,5656), c(4546,4646,464))
> List
[[1]]
[1] 123  NA  NA

[[2]]
[1] 1223 4546 5656

[[3]]
[1] 4546 4646  464

> data.frame(do.call(rbind, List))

    X1   X2   X3
1  123   NA   NA
2 1223 4546 5656
3 4546 4646  464
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
5

The do.call(rbind...) approach mentioned by Jilber is probably the most common or intuitive (and faster than the following approach), but you weren't too far off if you wanted to use unlist.

The trick is to use matrix() in conjunction with unlist, setting the number of rows to be the length of your list.

List <- list(c(123, NA, NA), c(1223, 4546,5656), c(4546,4646,464))
matrix(unlist(List), nrow = length(List), byrow = TRUE)
#      [,1] [,2] [,3]
# [1,]  123   NA   NA
# [2,] 1223 4546 5656
# [3,] 4546 4646  464

## Or, for a data.frame
data.frame(matrix(unlist(List), nrow = length(List), byrow = TRUE))
Community
  • 1
  • 1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
4
library(plyr)
ldply(list_name)

> x <- c(1,2,3)
> foo <- list(x,x,x)
> foo
[[1]]
[1] 1 2 3

[[2]]
[1] 1 2 3

[[3]]
[1] 1 2 3

> ldply(foo)
  V1 V2 V3
1  1  2  3
2  1  2  3
3  1  2  3
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
2

R data.frames are actually lists, which makes this an easy task if each vector in your list is a column:

List <- list(c(123, NA, NA), c(1223, 4546,5656), c(4546,4646,464))
DF <- data.frame(List)

In the case where each element of the list is a vector, you can use do.call and rbind as show by Jilber, or transpose the above data.frame:

DF <- data.frame(t(DF))
Zach
  • 29,791
  • 35
  • 142
  • 201