Home > front end >  The fastest way to import datasets with comomn characters in their names?
The fastest way to import datasets with comomn characters in their names?

Time:10-23

I have the following sets of data into the directory:

enter image description here

As you could see they are named with common chacrters they share each other. Could anyone suggest any possible way to import them all together in one round?

CodePudding user response:

list.files with pattern would give the name of the files in the directory, you may use lapply/map to import them together.

#select files that start with RP and end with extension xls.
filenames <- list.files(pattern = '^RP.*\\.xls$')
data <- purrr::map(filenames, readxl::read_excel)

If all the files have same column names and you would like to import them as one combined dataframe then use purrr::map_df instead of purrr::map.

CodePudding user response:

You can use list.files in collect your files and assign to create variable table names:

files <- list.files(pattern = "\\.xls")
for (i in 1:length(files)) {
  assign(gsub("\\.xls", "", files[i]), readxl::read_xls(files[i]))
}
  • Related