Home > Software engineering >  Iterate over multiple subdirectories to read.csv of a specific file
Iterate over multiple subdirectories to read.csv of a specific file

Time:01-28

I have a folder with over 100 sub-folders that each contain a specific csv "cats.csv" that I need to read into R.

so far I've got:

parent_folder <- "path of parent files"
sub_folders <- list.dirs(parent_folder, recursive = TRUE)[-1]
cat_files <- dir(sub_folders, recursive = TRUE, full.names = TRUE, pattern = "cats")

I've then tried variations of lapply and map to apply read.csv to load in all of the cat_files but it doesn't seem to work.

CodePudding user response:

filelist <- list.files(pattern = "cats.csv", recursive = TRUE, full.names = TRUE)

then

lapply(setNames(nm=filelist), read.csv)

edit with thanks to r2evans below

CodePudding user response:

We get the paths using Sys.glob (check that to be sure it is what you want) and then use Map to get a named list, DFs, of data.frames with the files' contents.

paths <- parent_folder |>
  file.path("*", "cats.csv") |>
  Sys.glob()

DFs <- Map(read.csv, paths)
  • Related