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

Using for_loop to combine multiple subset datasets

I want to select the same 3 variables from dflist 1-4. dflist is the list containing 4 datasets I have already loaded. i wrote a code to achieve it. My code like this:

dfmul<-rbind(dflist[[1]][,c("siteid_public","childid_public","round")], 
                dflist[[2]][,c("siteid_public","childid_public","round")], 
                dflist[[3]][,c("siteid_public","childid_public","round")], 
                dflist[[4]][,c("siteid_public","childid_public","round")])

But this looks very chunky and not efficient. Can someone help to figure out how to I simplify it? Thanks a lot~~!

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 :

You can loop over your list using lapply, select your variables and bind all resulting rows.

dfmul <- do.call(rbind, lapply(dflist, "[", c("siteid_public",
                                              "childid_public",
                                              "round")))
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