I'm using lapply
to read in multiple .xls
files from a directory. Since the data represents data collected from sites with a different ID given by the filename, I'd like to set the list name to be the filename of each file.
I am currently doing the following:
library(readxl)
# Set filepath
file_location="FILEPATH"
# List all files within folder
filenames=list.files(file_location, pattern="^ID.*xls",full.names = T)
# Import all files
import_data=lapply(filenames, function(x) read_xls(x, col_names = T))
I could then run something like this:
filenames_short=gsub(".xls", "", x=list.files(file_location, pattern="^ID.*xls",full.names = F))
names(import_data)=filenames_short
However, my pessimistic self is telling me that there is a possibility that the order of the filenames won't match the order of the list. Surely there must be a way to set this in the original command?