【问题标题】:Replace all values of NULL in a list of List to something else/ list of list with varying lengths将列表列表中的所有 NULL 值替换为其他内容/具有不同长度的列表列表
【发布时间】:2016-06-10 18:51:49
【问题描述】:

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

行的位置很重要,这样我以后可以将它们链接到另一个项目。我尝试了一种将其放入数据框的方法,但由于某些行为 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.

【问题讨论】:

    标签: r list plyr nested-lists


    【解决方案1】:
    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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多