取消列出data.table中的嵌套列表列

时间:2023-02-02 20:27:06

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 opposite operation to data.table aggregation to list column.
I think it is worth to have it in SO knowledge base.
My current workaround approach below, I'm looking for a little bit more canonical answer.

取消列出data.table中的嵌套列表列。假设所有列表元素都是相同的类型。列表元素已命名,名称也必须处理。它在某种程度上与data.table聚合到列表列的操作相反。我认为值得在SO data.table知识库中使用它。我目前的解决方法方法如下,我正在寻找更多的规范答案。

library(data.table)
dt <- data.table(
    a = letters[1:3], 
    l = list(list(c1=6L, c2=4L), list(x=2L, y=4L, z=3L), list())
)
dt[]
#    a      l
# 1: a <list>
# 2: b <list>
# 3: c <list>
dt[,.(a = rep(a,length(l)),
      nm = names(unlist(l)),
      ul = unlist(l)),
   .(id = seq_along(a))
   ][, id := NULL
     ][]
#    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

1 个解决方案

#1


9  

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):

不确定它是更“规范”但是这里是一种修改l的方法,所以你可以使用by = a,考虑到你知道列表中数据的类型(有了一些改进,感谢@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

#1


9  

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):

不确定它是更“规范”但是这里是一种修改l的方法,所以你可以使用by = a,考虑到你知道列表中数据的类型(有了一些改进,感谢@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