Search code examples
rdplyrtidyversepurrrrdata

Loading multiple .RData and binding into a single data.frame


I have multiple .RData in a folder. I can load them as follows:

df1 <- data.frame(X = 1:10)
df2 <- data.frame(X = 1:10)

save(df1, file = "df1.RData", compress = "xz")
save(df2, file = "df2.RData", compress = "xz")

rm(list = ls())

load("df1.RData")
load("df2.RData")

and bind them using bind_rows function from dplyr as

library(tidyverse)
bind_rows(df1, df2)

I wonder if it is possible to load all files and bind them something like this

list.files(
      path      = "."
    , pattern    = "*.RData"
    , full.names = TRUE
    )  %>%
  map_dfr(.x = ., .f = ~load(file = .x))

Solution

  • You could use get() to return the data from the calling environment or alternatively load them into a new environment and bind them afterwards. Note that .Rdata files can contain multiple objects but assuming these objects are all conformable, you could do:

    library(purrr)
    library(dplyr)
    
    df1 <- data.frame(X = 1:10)
    df2 <- data.frame(X = 1:10)
    
    save(df1, file = "df1.RData", compress = "xz")
    save(df2, file = "df2.RData", compress = "xz")
    
    list.files(pattern = "\\.RData$") %>%
       map_df(~ get(load(file = .x)))
    
        X
    1   1
    2   2
    3   3
    4   4
    5   5
    6   6
    7   7
    8   8
    9   9
    10 10
    11  1
    12  2
    13  3
    14  4
    15  5
    16  6
    17  7
    18  8
    19  9
    20 10 
    

    Or:

    temp_env <- new.env()
    
    list.files(pattern = "\\.RData$")  %>%
      map(~load(file = .x, envir = temp_env)) 
    
    bind_rows(as.list(temp_env))