cbind with partially nested list

Viewed 266

I'm trying to cbind or unnest or as.data.table a partially nested list.

id <- c(1,2)
A <- c("A1","A2","A3")
B <- c("B1")
AB <- list(A=A,B=B)
ABAB <- list(AB,AB)
nested_list <- list(id=id,ABAB=ABAB)

The length of id is the same as ABAB (2 in this case). I don't know how to unlist a part of this list (ABAB) and cbind another part (id). Here's my desired result as a data.table:

data.table(id=c(1,1,1,2,2,2),A=c("A1","A2","A3","A1","A2","A3"),B=rep("B1",6))
   id  A  B
1:  1 A1 B1
2:  1 A2 B1
3:  1 A3 B1
4:  2 A1 B1
5:  2 A2 B1
6:  2 A3 B1
5 Answers
Related