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 data.table knowledge base.
My current workaround approach below, I'm looking for a little bit more canonical answer.
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