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

Flatten deeply nested list of dataframes

Consider this nested list of dataframes:

df <- data.frame(x = 1:5, y = letters[1:5])
l <- list(df, list(df, df), list(df, list(df, df, list(df))), list(df), df)

How can one get from this deeply nested list to a simple list of dataframes:

list(df, df, df, df, df, df, df, df, df)

Usual solutions (like here) fails to keep dataframes’ structure.

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

>Solution :

A convenient option is to use rrapply:

rrapply(l, classes = "data.frame", how = "flatten")

Check whether it’s the same as the desired output:

identical(list(df, df, df, df, df, df, df, df, df), 
          rrapply(l, classes = "data.frame", how = "flatten"))

[1] TRUE
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