data.frame안에 리스트나 데이터프레임을 다중으로 넣는 경우가 종종 발생하는 것 같다. R에 jsonlite패키지에서 JSON파일을 불러 왔을 때도 이런 유형의 구조인 것 같다. 해당 자료를 만드는 방법은 여러가지가 될 것 같다.
가장 먼저 생각난 방식은 아래와 같다. 잘 생성되나 더 좋은 방법이 없을까 고민이 된다. 구글링을 하다보니 여러가지 방법들이 더 존재하였다.
list_item=list(a=1:3,b=1:4)
df=data.frame(remove=1:length(list_item))
df$list=list_item
df$remove=NULL
리스트를 포함한 data.frame 생성
I, list2DF 함수 활용
I함수를 list에 추가로 씌우게 되면 리스트를 보호하며 data.frame을 생성할 수 있다고 한다.
data.frame(list=I(list(a=1:3,b=1:4)))
list2DF(list(a=1:3, b=list(1:1, 1:2, 1:3)))
다른 패키지 활용
library(data.table)
data.table(list=list(a=1:3,b=1:4))
향후 계획
data.frame안에 data.frame 을 넣는게 위 방식으로는 잘 적용되지 않았다. 좀더 명료한 방안이 생기면 포스팅을 수정할 계획이다.
# 적용 안됨
data.frame(list=I(list(a=1:3,b=1:4)),df=I(iris[1:2,]))
#적용 방안
df=data.frame(list=I(list(a=1:3,b=1:4)))
df$df=iris[1:2,]
참고
Create a data.frame where a column is a list
I know how to add a list column: > df <- data.frame(a=1:3)> df$b <- list(1:1, 1:2, 1:3)> df a b1 1 12 2 1, 23 3 1, 2, 3 This works, but not: > df <- data.

Populating data frame cells with more than one value | R-bloggers
Data frames are lists Most R users will know that data frames are lists. You can easily verify that a data frame is a list by typing However, data frames are lists with some special properties. For example, all entries in the list must have the same length (here 2), etc. ...