Importing multiple csv files with read_csv in r

We can use map

library(purrr)
library(readr)
out_list <- map(data, read_csv)
names(out_list) <- sub("\\.csv", "", data)
list2env(out_list, .GlobalEnv) # not recommended

Or using a for loop, create a NULL list to store the output of the read data, then loop over the ‘data’, read them and assign it to the output list element

out_list <- vector('list', length(data))
names(out_list) <- data
for(file in data) out_list[[file]] <- read_csv(file)

If we want to create multiple new objects (not recommended)

for(file in data) {
    assign(sub("\\.csv", "", file), read_csv(file))
 }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top