Convert R list to dataframe with missing/NULL elements

a 夏天 提交于 2019-11-27 02:09:26

问题


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...


回答1:


A comment mentioned wanting only a single loop, which can be achieved with @flodel's answer just by putting the body of the two loops together:

rbind.fill(lapply(alist, function(f) {
  as.data.frame(Filter(Negate(is.null), f))
}))

giving

  name age
1  Foo  22
2  Bar  NA
3  Baz  NA



回答2:


Step1: remove NULL items

non.null.list <- lapply(alist, Filter, f = Negate(is.null))

Step2: stack everything together:

library(plyr)
rbind.fill(lapply(non.null.list, as.data.frame))
#   name age
# 1  Foo  22
# 2  Bar  NA
# 3  Baz  NA

Edit: In case you had a variable that is NULL for all your list items, it would not show up in your final output. If instead, you'd like a column filled with NA, the first step should not remove NULLs but replace them with NAs:

Step 1 alternative: replace NULL with NA:

non.null.list <- lapply(alist, lapply, function(x)ifelse(is.null(x), NA, x))


来源:https://stackoverflow.com/questions/15793759/convert-r-list-to-dataframe-with-missing-null-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!