Given a list:
alist = list(
list(name="Foo",age=22),
list(name="Bar"),
list(name="Baz",age=NULL)
)
what's the best way to convert this into a dataframe with name and age columns, with missing values (I'll accept NA or "" in that order of preference)?
Simple methods using ldply
fail because it tries to convert each list element into a data frame, but the one with the NULL barfs because the lengths don't match. Best I have at the moment is:
> ldply(alist,function(s){t(data.frame(unlist(s)))})
name age
1 Foo 22
2 Bar <NA>
3 Baz <NA>
but that's pretty icky and the numeric variable becomes a factor...