I have a series of complex lists that I am trying to convert to data frame format in R. Each list will be converted to a data frame individually. Some of the lists have some values that are blank, making it hard to convert the list to a data frame.
I have tried lapply/ sapply to find the maximum length of the list and neither work. Additionally, using do.call and unlist does not give me the data in the format I want (it loses column titles and formats the table into three columns, rather than many columns). I found that the following code works on the first 5 lists:
max_list<-max(rapply(List, length))
df<-as.data.frame(rapply(List, function(x) 'length<-'(x, max_list), how="list"))
However, when I try to apply this in a loop for all of the lists, I get the following error.
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 3, 0
My ultimate goal is for the parts of the list that have missing values to be converted to "NA."
Is there anything I can add to this code to avoid the error? Or is there something else that I can do to convert those lists that have differing numbers of rows?
EDIT:
I can't post my exact code for the inputs online but the list is nested and is very complex.
Example input:
iter1 <- list(item1 = 1, item2 = "a",item3="b")
iter2 <- list(item1 = 2, item2 = "c")
List <- list(iter1 = iter1, iter2 = iter2)
Ideally, I'd like for this output as a data frame:
V1 V2 V3 V4 V5
1 a b 2 c
As mentioned, I was able to get this result with the code above. But, I'm not sure why the code is failing when applied to more complex lists.