bind multiple R RDS files

How to load and combine multiple R RDS files

Saving data into an RDS file is one of my favorite ways to store data, but what if it is necessary to load and combine multiple R RDS files at once? Here is how to do that fast and easily.

Loading one RDS file is easy with the function readRDS.

By using the map_dfr function from the purrr package, you can load and combine multiple RDS files. That will easily combine them by rows.

You can create a list of file paths like this and do some R piping.

require(tidyverse)

rdsfiles <-
  c(
    "C:/myfiles/df_Q1.rds",
    "C:/otherfiles/df_Q2.rds"
  )

rds_combo <- rdsfiles %>%
  map_dfr(readRDS)

Or, if you have it all in one place, you can do that by using the list.files function.

require(tidyverse)

rds_combo <- list.files( path = "~/rds_files_location/", pattern = "*.rds", full.names = TRUE ) %>%
  map_dfr(readRDS)

There is also function map_dfc if you want to bind columns from the rds file content.

Here is another approach on how to combine data that are in separate files with an additional column that contains a file name.

Check out my favorite RStudio tips and tricks. For example, how to comment out multiple lines of R script at once.





Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *