Importing many CSV files

I already uploaded the 25 files into a folder in R Studio called “march_data"

Is there a way of importing them all at once instead of just doing this over and over for each one:

house_march <- read_csv("march_data/house_march.csv")
weather_march <- read_csv("march_data/weather_march.csv")

Here are two ways to read the csv files. One reads the files into a list and the other reads them into individual data frames.

library(readr)
#get all file names
FILES <- list.files(path = "march_data", pattern = "csv$")

#read into a list
AllData <- lapply(FILES, read_csv)
names(AllData) <- FILES

# or individual files
for(Nm in FILES) {
  assign(Nm, read_csv(Nm))
}
2 Likes

A simpler method:

library(readr)
#get all file names
FILES <- list.files(path = "march_data", pattern = "csv$")

AllData <- read_csv(FILES)
2 Likes

This worked thanks. FYI for any others I first ran.

FILES <- list.files(path = "march_data", pattern = "csv$")

Then when I tried to run the next code (I went with read into individual files) I got an error
saying something along the lines of files not found in the directory "/cloud/project" so I just set my march_data folder as the Working Directory by using

setwd("/cloud/project/march_data")

then went ahead and ran the code for individual files.

Note that since its a lot of csv files, running it for individual files will have it take up a lot of rows in the Environment Panel. But I personally prefer having it like that so I don't mind.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.