for a large number of files which may need to be operated on in a similar process and with well structured file names.
firstly a vector of the file names to be accessed must be created, there are multiple options for this:
Creating the vector manually with
files <- paste0("file_", 1:100, ".rds")
list.files() with a regex search term for the file type, requires knowledge of regular expressions (regex) if other files of same type are in the directory.
files <- list.files("./", pattern = "\\.rds$", full.names = TRUE)
X is a vector of part of the files naming format used.
lapply will output each response as element of a list.
readRDS is specific to
.rds files and will change depending on the application of the process.
my_file_list <- lapply(files, readRDS)
This is not necessarily faster than a for loop from testing but allows all files to be an element of a list without assigning them explicitly.
Finally, we often need to load multiple packages at once.
This trick can do it quite easily by applying
library() to all libraries that we wish to import: