In a simple case of all of the files having the same column structure and being stored in the current working directory, the code can look like this.
library(openxlsx) library(dplyr) FileNames <- list.files(path = ".", pattern = "xlsx$") FILES <- lapply(FileNames, read.xlsx) AllDat <- bind_rows(FILES)
The data frame AllDat contains all of the data.
Thank you for the help! As a follow up, what would be the syntax of filling in the multiple files names in the path section of the list.files code? ex: FilesNames <- list.files(path = "tripdat1.xlsx", "tripdat2.xlsx", "tripdat3.xlsx", etc., pattern - "xlsx$")
The path argument determines which directory will be searched for the files. The pattern argument determines, with a regular expression, which file names will be matched. I used
xlsx$ so that every file whose name ends in xlsx will be listed. If you want only files whose name starts with tripdat and ends with xlsx, you could use
pattern = "tripdat.*xlsx$". Regular expressions are very powerful and you can make complex criteria if you need to. If you get stuck coming up with the correct regular expression, please describe the file names you need to match and someone will probably be able to provide a regular expression.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.