loop through all files in a folder in r

You can use the list.files() function to get all the file names in a folder and then loop through them using a for loop or lapply() function. Here is an example:

main.r
# set the path to the folder
path <- "/path/to/folder/"

# get all the file names in the folder
files <- list.files(path)

# loop through each file and read it in
for (file in files) {
  data <- read.csv(paste0(path, file))
  # do some processing on the data
}

# or use lapply() function
data_list <- lapply(files, function(file) {
  read.csv(paste0(path, file))
})
367 chars
17 lines

related categories

gistlibby LogSnag