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

split file names into list of lists in r

I have a bunch of csv file names that I need to read into R and split into a list of lists based on the file name. Say my files are named:

"gam_1-1.csv"
"gam_1-2.csv"
"gam_2-1.csv"
"gam_2-2.csv"

How do I separate them so that I have a list of lists with the file names, where:

list1 <- list("gam_1-1.csv","gam_1-2.csv")
list2 <- list("gam_2-1.csv","gam_2-2.csv")
final_list <- list(list1,list2)

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 split on a substring of the filenames.

files <- c("gam_1-1.csv", "gam_1-2.csv", "gam_2-1.csv", "gam_2-2.csv")
split(files, sub("-.*", "", files))
# $gam_1
# [1] "gam_1-1.csv" "gam_1-2.csv"
# $gam_2
# [1] "gam_2-1.csv" "gam_2-2.csv"
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