Read into multiple .csv files

Viewed 62

I would like to read into csv. files from a specific location. How do I read multiple files into the global environment at once? How do I unpack the contents of a list of data frames into it, or vice versa?

2 Answers

You can read the data in a list.

filenames <- list.files(pattern = 'csv$', full.names = TRUE)
list_data <- lapply(filenames, readr::read_csv)

It is not recommended to create multiple objects in global environment (like df1, df2 etc) because they are difficult to manage but if you need them you can use list2env

names(list_data) <- paste0('df', seq_along(list_data))
list2env(list_data, .GlobalEnv)

Not sure if I understand your question properly, does this work?

tables_to_read <- list()

for (i in 1:10) {
     tables_to_read[i] <- read_csv(paste0(i, '.csv'))
 }
Related