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

Convert Each Element in List to a Column in a Data Frame

Suppose I have the following list "d":

library(combinat)
d = permn(c("a", "b", "c"))

This looks as follows:

[[1]]
[1] "a" "b" "c"

[[2]]
[1] "a" "c" "b"

[[3]]
[1] "c" "a" "b"

[[4]]
[1] "c" "b" "a"

[[5]]
[1] "b" "c" "a"

[[6]]
[1] "b" "a" "c"

Is it possible to convert each element of this list into a column?

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

This is what I am trying to do:

desired_result = data.frame(col1 = c("a", "b", "c"), col2 = c("a", "c", "b"), col3 = c("c", "a", "b"), col4 = c("c", "b", "a"), col5 = c("b", "c", "a"), col6 = c("b", "a", "c"))

 col1 col2 col3 col4 col5 col6
1    a    a    c    c    b    b
2    b    c    a    b    c    a
3    c    b    b    a    a    c

Can someone please show me how to do this?

Thanks!

>Solution :

Try matrix(unlist(d), ncol = length(d)) or, if you need a data frame, data.frame(matrix(unlist(d), ncol = length(d)) for any length of d

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