简体   繁体   中英

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!

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM