user5512599
user5512599

Reputation: 1

Is there a way to read multiple CSV files from multiple folders in R?

I am running a data analysis. I have a main folder comprised of 100 subfolders (1 subfolder for each user response I am analysing). Each subfolder has ~50 csv files. Is there a way I can read all the csv files across all the subfolders in one go and create 1 combined dataframe?

Thank you!

Upvotes: 0

Views: 1613

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269586

Assuming that the file names all have an extension of .csv, compute the paths to all the csv files and then use Map to use read.csv with each path. This will create a list L of data frames. The list names will be the paths. If the csv files all have the same columns then we can use the last line to create a single data frame. It will have a path column which identifies which path each row came from.

paths <- Sys.glob("*/*.csv")
L <- Map(read.csv, paths)

dplyr::bind_rows(L, .id = "path")

or expressed in a pipeline

library(dplyr)

"*/*.csv" %>%
  Sys.glob %>%
  Map(read.csv, .) %>%
  bind_rows(.id = "path")

Upvotes: 1

Related Questions