sym246
sym246

Reputation: 1866

Use filename as list name using lapply and read_xls

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?

Upvotes: 3

Views: 1328

Answers (1)

I agree with @jogo, but if this generates insecurity, you can return the table with the name of the file.

One option is to add an attribute to the table:

import_data=lapply(filenames, function(x) {
                   out <- read_xls(x, col_names = T)
                   attr(out, "file") <- x
                   return(out)
                   }) 

Another is to return a list where the table is an object and it is already named.

import_data=lapply(filenames, function(x) {
                   out <- list(read_xls(x, col_names = T))
                   names(out) <- gsub(".xls", "", x)
                   return(out)
                   }) 

Upvotes: 4

Related Questions