I don't know the structure of your .RDS files, but I would probably do this in three steps. This approach adds in the filename in a column based on what's read in so you know which file the data came from.
- Create a vector of filenames with full paths, with the
here package to specify the root directory.
library("here")
library("readr")
library("purrr")
library("fs")
dir_list <- list.files(here("data/source1"),
pattern = "*.RDS", full.names = TRUE)
- Name the vector using just the filename without the extension.
names(dir_list) <- path_ext_remove(list.files(here("data/source1"),
pattern = "*.RDS"))
- Read in the files into a single list and combine them.
file_list <- map(dir_list, read_rds)
files_df <- bind_rows(file_list, .id = "source")
These last 2 can also be shortened as
files_df <- map_dfr(dir_list, read_rds, .id = "source")