Unlist nested list columns in data.table

后端 未结 1 678
梦毁少年i
梦毁少年i 2021-02-08 15:47

Unlist nested list column in data.table. Assuming all the list elements are the same type. The list elements are named, the name has to be handled also.
It is somehow opposi

1条回答
  •  情歌与酒
    2021-02-08 16:36

    Not sure it is more "canonical" but here is a way to modify l so you can use by=a, considering you know the type of your data in list (with some improvements, thanks to @DavidArenburg):

    dt[lengths(l) == 0, l := NA_integer_][, .(nm = names(unlist(l)), ul = unlist(l)), by = a]
    
    #   a nm ul
    #1: a c1  6
    #2: a c2  4
    #3: b  x  2
    #4: b  y  4
    #5: b  z  3
    #6: c NA NA
    

    0 讨论(0)
提交回复
热议问题