将 List 列表中的所有 NULL 值替换为其他内容/具有不同长度的列表列表
Replace all values of NULL in a list of List to something else/ list of list with varying lengths
我有一个列表列表,我需要将其转换为相应的数据框。
行的位置很重要,这样我可以稍后将它们 link 到另一个项目。我尝试了一种将其放入数据框的方法,但由于某些行为 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.
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)))
我有一个列表列表,我需要将其转换为相应的数据框。
行的位置很重要,这样我可以稍后将它们 link 到另一个项目。我尝试了一种将其放入数据框的方法,但由于某些行为 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.
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)))