0

I have a list of lists and I want to convert it into a dataframe. The challenge is that there are missing variables names in lists (not NA's but the variable is missing completely).

To illustrate on example: from

my_list <- list() 
my_list[[1]] <- list(a = 1, b = 2, c = 3)
my_list[[2]] <- list(a = 4, c = 6)

I would like to get

     a  b c
[1,] 1  2 3
[2,] 4 NA 6
Love-R
  • 798
  • 6
  • 18

3 Answers3

2

Another option is

library(reshape2)
as.data.frame(acast(melt(my_list), L1~L2, value.var='value'))
#  a  b c
#1 1  2 3
#2 4 NA 6

Or as @David Arenburg suggested a wrapper for melt/dcast would be recast

recast(my_list, L1 ~ L2, value.var = 'value')[, -1]
#  a  b c
#1 1  2 3
#2 4 NA 6
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    Or maybe `recast(my_list, L1 ~ L2, value.var = 'value')[, -1]`, though the `-1` is ugly probably. – David Arenburg Jun 23 '15 at 09:39
  • @DavidArenburg Thanks, I tried that before, but there is not an option with a `acast` matrix output – akrun Jun 23 '15 at 09:40
  • 1
    OP explicitly said they want to convert it to a data frame. – David Arenburg Jun 23 '15 at 09:52
  • @DavidArenburg Thanks, just noticed that. I was looking at his matrix output – akrun Jun 23 '15 at 09:54
  • 1
    @akrun You might wish to add your nice suggestion to [**this Q&A**](http://stackoverflow.com/questions/3402371/rbind-different-number-of-columns) which seems like a possible canonical answer for similar questions. – Henrik Jun 23 '15 at 09:55
  • @DavidArenburg Please do add all the suggestions here in the link Henrik suggested. – akrun Jun 23 '15 at 09:57
1

You can use the bind_rows function from the dplyr package :

my_list <- list() 
my_list[[1]] <- list(a = 1, b = 2, c = 3)
my_list[[2]] <- list(a = 4, c = 6)
dplyr::bind_rows(lapply(my_list, as.data.frame))

This outputs:

Source: local data frame [2 x 3]
  a  b c
1 1  2 3
2 4 NA 6
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Tutuchan
  • 1,527
  • 10
  • 19
  • 3
    Or `data.table::rbindlist(lapply(my_list, as.data.frame), fill = TRUE)` – David Arenburg Jun 23 '15 at 09:36
  • @DavidArenburg You might wish to add your nice suggestion to [**this Q&A**](http://stackoverflow.com/questions/3402371/rbind-different-number-of-columns) which seems like a possible canonical answer for similar questions. – Henrik Jun 23 '15 at 09:54
1

Another answer, this requires to change the class of the arguments to data.frames:

 library(plyr)
 lista <- list(a=1, b=2, c =3)
 listb <- list(a=4, c=6)
 lista <- as.data.frame(lista)
 listb <- as.data.frame(listb)
 my_list <- list(lista, listb)
 my_list <- do.call(rbind.fill, my_list)
 my_list
   a  b c
 1 1  2 3
 2 4 NA 6
erasmortg
  • 3,246
  • 1
  • 17
  • 34
  • BTW, you are not using the example data structure provided by the OP. If you are creating a new structure, you would use something similar as @Tutuchan's code rather than manually changing/creating. – akrun Jun 23 '15 at 09:45
  • 1
    This is way to manual. OP already provided the data. All you need to do is just `my_list <- lapply(my_list, as.data.frame) ; do.call(rbind.fill, my_list)` – David Arenburg Jun 23 '15 at 10:37