将List列表中的所有NULL值替换为具有不同长度的其他/列表列表

时间:2016-06-10 18:51:49

标签: r list plyr nested-lists

我有一个列表,我需要将其转换为相应的数据帧。

行的位置很重要,以便稍后我可以将它们链接到另一个项目。我尝试了一种方法,我把它变成了一个数据帧,但我不能这样做,因为有些行是NULL。

  first = c(1,2,3)
  second = c(1,2)
  problemrows = NULL

  mylist = list(first,second,problemrows)
  #mylist   - should turn into a dataframe with each row being same order as        the list of lists.  NULL value would be a null row

 library(plyr) # doesn't work because of NULLs above
    t(plyr::rbind.fill.matrix(lapply(mylist, t)))
  ## help^^^   Ideally the row that doesn't work would just be a null row or the new dataframe would remove these NULL lists as rows and instead assign appropriate row#s to everything else to make up for what was skipped.


     # other approach - change all the NULL list of lists to something like -999 which is another fix.

1 个答案:

答案 0 :(得分:2)

first = c(1,2,3)
second = c(1,2)
problemrows = NULL

mylist = list(first,second,problemrows)

mylist <- sapply(mylist, function(x) ifelse(x == "NULL", NA, x))

library(plyr) # doesn't work because of NULLs above
t(plyr::rbind.fill.matrix(lapply(mylist, t)))