Convert Mixed-Length named List to data.frame

前端 未结 6 1543
夕颜
夕颜 2020-12-31 04:52

I have a list of the following format:

[[1]]
[[1]]$a
[1] 1

[[1]]$b
[1] 3

[[1]]$c
[1] 5

[[2]]       
[[2]]$c
[1] 2

[[2]]$a
[1] 3

There i

6条回答
  •  悲哀的现实
    2020-12-31 05:04

    I know this is an old question, but I just came across it and it's excruciating not to see the simplest solution I'm aware of. So here it is (simply specify 'fill=TRUE' in rbindlist):

    library(data.table)
    list = list(list(a=1,b=3,c=5),list(c=2,a=3))
    rbindlist(list,fill=TRUE)
    
    #    a  b c
    # 1: 1  3 5
    # 2: 3 NA 2
    

    I don't know if this is the fastest way, but I'd be willing to bet that it competes, given data.table's thoughtful design and extremely good performance on a lot of other tasks.

提交回复
热议问题