Adam_G
Adam_G

Reputation: 7879

cbind a column containing lists maintaining column of lists

I am trying to cbind a dataframe to a column, where some of the rows of that column are made up of lists. For example:

df1 <- tibble(x1=c(1,2,3))
df2 <- tibble(x2=c(4,5,6),
              x.list=list(list(7,8),9,list(10,11,12)))

But when I try to cbind just the column of lists:

df3 <- cbind(df1,df2$x.list)

I get an unnested(?) version of xlist:

7:30:10> df3
  x1 7 8 9 10 11 12
1  1 7 8 9 10 11 12
2  2 7 8 9 10 11 12
3  3 7 8 9 10 11 12

How can I cbind the column of lists and maintain it as a single column of lists?

Upvotes: 2

Views: 41

Answers (1)

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

You can try this based on the deference between $ and [ where the later returns a list

df3 <- cbind(df1,df2[2])
  • output
  x1     x.list
1  1       7, 8
2  2          9
3  3 10, 11, 12

we can see that class(df3$x.list) is list

Upvotes: 3

Related Questions