I have 10 Excel files that I want to read into R, with the following names:
- Data - January 2021.xlsx
- Data - February 2021.xlsx
- Data - March 2021.xlsx
- Data - April 2021.xlsx
- Data - May 2021.xlsx
- Data - June 2021.xlsx
- Data - July 2021.xlsx
- Data - August 2021.xlsx
- Data - September 2021.xlsx
- Data - October 2021.xlsx
I am able to write R code to do so 10 times, once for each month, as follows:
data_January_2021 <- read.xlsx("Data - January 2021.xlsx")
data_February_2021 <- read.xlsx("Data - February 2021.xlsx")
data_March_2021 <- read.xlsx("Data - March 2021.xlsx")
data_April_2021 <- read.xlsx("Data - April 2021.xlsx")
data_May_2021 <- read.xlsx("Data - May 2021.xlsx")
data_June_2021 <- read.xlsx("Data - June 2021.xlsx")
data_July_2021 <- read.xlsx("Data - July 2021.xlsx")
data_August_2021 <- read.xlsx("Data - August 2021.xlsx")
data_September_2021 <- read.xlsx("Data - September 2021.xlsx")
data_October_2021 <- read.xlsx("Data - October 2021.xlsx")
However, since the file names differ only by month, I decided to write a for loop to streamline to process, as follows.
months <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October")
for (i in 1:length(months)) {
data_[i]_2021 <- read.xlsx(paste0("Data - ", months[i], " 2021.xlsx"))
}
Again, the goal of this for loop is to create the following data frames in the R environment:
data_January_2021
data_February_2021
data_March_2021
data_April_2021
data_May_2021
data_June_2021
data_July_2021
data_August_2021
data_September_2021
data_October_2021
When I run this, however, I get the following error:
Error: object 'data_' not found
How would I fix the for loop to do just that?
CodePudding user response:
We can use lapply
and list.files
to create a list of dataframes.
lapply(list.files(pattern="Data.*2021\\.xlsx"), \(x) readxl::read_xlsx(x))