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

How do I convert a hierarchical dataframe to a list in R?

If I had a hierarchical dataframe like this:

level_1<-c("a","a","a","b","c","c")
level_2<-c("flower","flower","tree","mushroom","dog","cat")
level_3<-c("rose","sunflower","pine",NA,"spaniel",NA)
level_4<-c("pink",NA,NA,NA,NA,NA)
df<-data.frame(level_1,level_2,level_3,level_4)

How do I convert this to a list which orders according to the hierarchy, like this:

> list
 [1] "a"         "flower"    "rose"      "pink"      "sunflower" "tree"      "pine"      "b"         "mushroom"  "c"        
[11] "dog"       "spaniel"   "c"         "cat"      

So for in value in level 1, it list all level 2 values expanded across the other levels. Hopefully that makes sense?

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

Thanks in advance!

>Solution :

In the question "c" appears twice in the desired answer but "a" and "b" only appear once. We assume that this is an error and what is wanted is that each should only appear once.

uniq <- function(x) unique(na.omit(c(t(x))))
unname(unlist(lapply(split(df, df$level_1), uniq)))
##  [1] "a"         "flower"    "rose"      "pink"      "sunflower" "tree"     
##  [7] "pine"      "b"         "mushroom"  "c"         "dog"       "spaniel"  
## [13] "cat"
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