Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading