Converting nested list to dataframe

后端 未结 3 2202
南旧
南旧 2020-11-27 03:14

The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is:



        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 04:12

    You could create a list of data.frames:

    dfs <- lapply(mylist, data.frame, stringsAsFactors = FALSE)
    

    Then use one of these:

    library(plyr)
    rbind.fill(dfs)
    

    or the faster

    library(dplyr)
    rbind_all(dfs)
    

    In the case of dplyr::rbind_all, I am surprised that it chooses to use "" instead of NA for missing data. If you remove stringsAsFactors = FALSE, you will get NA but at the cost of a warning... So suppressWarnings(rbind_all(lapply(mylist, data.frame))) would be an ugly but fast solution.

提交回复
热议问题