Rfanatic
Rfanatic

Reputation: 2290

Read into multiple .csv files

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?

Upvotes: 2

Views: 78

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389265

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)

Upvotes: 4

CodingBiology
CodingBiology

Reputation: 272

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'))
 }

Upvotes: 1

Related Questions