简体   繁体   中英

add a data frame to an existing rdata file

I am fairly new to R and will try my best to make myself understood. Suppose if I have an existing rdata file with multiple objects. Now I want to add a data frame to it how do i do that? I tried the following:

write.data.loc <- 'users/Jim/Objects'

rdataPath <- 'users/Jim/Objects.Rda'

myFile<- read.csv("myFile.csv")

loadObjects <- load(rdataPath)

save(loadObjects,myFile,file=paste(write.data.loc,".Rda",sep=""))

But this does not seem to work?

I'm not certain of your actual use-case, but if you must "append" a new object to an rda file, here is one method. This tries to be clever by load ing all of the objects from the rda file into a new environment (there are many tutorials and guides that discuss the use and relevance of environments, Hadley's "Advanced R" is one that does a good job, I think).

This first step loads all of the objects into a new (empty) environment. It's useful to use an otherwise-empty environment so that we can get all of the objects from it rather easily using ls .

e <- new.env(parent = emptyenv())
load("path/to/.rda", envir = e)

The object you want to add should be loaded into a variable within the environment. Note that the dollar-sign access looks the same as list s, which makes it both (1) easy to confuse the two, and (2) easy to understand the named indexing that $ provides.

e$myFile <- read.csv("yourFile.csv")

This last piece, re-saving the rda file, is an indirect method. The ls(envir = e) returns the variable names of all objects within the environment. This is good, because save can deal with objects or with their names.

do.call("save", c(ls(envir = e), list(envir = e, file = "newsave.rda")))

Realize that this is not technically appending the data.frame to the rda file, it's over-writing the rda file with a new one that happens to contain all the previous objects and the new one data.frame.

I wrote this solution that can add dataframes, list, matrices or lists. By default it will overwrite an existing object but can be reversed with overwrite=TRUE .

add_object_to_rda <- function(obj, rda_file, overwrite = FALSE) {
    .dummy <- NULL
    if (!file.exists(rda_file)) save(.dummy, file = rda_file)

    old_e <- new.env()
    new_e <- new.env()

    load(file = rda_file, envir = old_e)

    name_obj <- deparse(substitute(obj))   # get the name of the object

    # new_e[[name_obj]] <- get(name_obj)     # use this only outside a function
    new_e[[name_obj]] <- obj

    # merge object from old environment with the new environment
    # ls(old_e) is a character vector of the object names
    if (overwrite) {
        # the old variables take precedence over the new ones
        invisible(sapply(ls(new_e), function(x)
            assign(x, get(x, envir = new_e), envir = old_e)))
        # And finally we save the variables in the environment
        save(list = ls(old_e), file = rda_file, envir = old_e)
    }
    else {
        invisible(sapply(ls(old_e), function(x)
            assign(x, get(x, envir = old_e), envir = new_e)))
        # And finally we save the variables in the environment
        save(list = ls(new_e), file = rda_file, envir = new_e)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM