Combine multiple .RData files containing objects with the same name into one single .RData file

后端 未结 2 1712
Happy的楠姐
Happy的楠姐 2020-12-16 05:57

I have many many .RData files containing one dataframe that I had saved in a previous analysis and the data frame has the same name for each file loaded. So for example usin

2条回答
  •  半阙折子戏
    2020-12-16 06:13

    I think the best answer I saw was the code below, which I copied from an SO answer which I can't track down right now. Apologies to the original author.

    resave <- function(..., list = character(), file) {
       previous  <- load(file)
       var.names <- c(list, as.character(substitute(list(...)))[-1L])
       for (var in var.names) assign(var, get(var, envir = parent.frame()))
       save(list = unique(c(previous, var.names)), file = file)
    }
    #I took advantage of the fact the load function 
    #returns the name of the loaded variables, so 
    #I could use the function's environment instead of creating one.
    #And when using get, I was careful to only look in the 
    #environment from which the function is called, i.e. parent.frame()
    

提交回复
热议问题