1

I have a variable out that is a list of lists and I want to format the first child list to a dataframe. Say my out looks like this:

[[1]]
[[1]]$id
[1] "1"

[[1]]$input
[1] "A" "B" "C" 

[[2]]
[[2]]$id
[1] "2"

[[2]]$input
[1] "R" "S" "T"

class(out) and class(out[[1]]) confirms that this is a list of lists.

I want to create a "long" dataframe that should look like this:

id    input
1     "A"
1     "B"
1     "C"
2     "R"
2     "S"
2     "T"

I tried:

lapply(out, function(x){
     as.data.frame(x)
   })

but this seems to do an cbind and creates new columns for each child list.

Any help is highly appreciated.

Jaap
  • 81,064
  • 34
  • 182
  • 193
ben_aaron
  • 1,504
  • 2
  • 19
  • 39

1 Answers1

1

try

library(plyr)
ldply(out, as.data.frame)
Richard Telford
  • 9,558
  • 6
  • 38
  • 51