xoxo
xoxo

Reputation: 53

Iterate over multiple subdirectories to read.csv of a specific file

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.

Upvotes: 0

Views: 69

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269421

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)

Upvotes: 0

PGSA
PGSA

Reputation: 3071

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

then

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

edit with thanks to r2evans below

Upvotes: 2

Related Questions