简体   繁体   English

将数据框添加到现有的rdata文件

[英]add a data frame to an existing rdata file

I am fairly new to R and will try my best to make myself understood. 我是R的新手,将尽我所能使自己理解。 Suppose if I have an existing rdata file with multiple objects. 假设我有一个包含多个对象的现有rdata文件。 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. 我不确定您的实际用例,但是如果必须将新对象“附加”到rda文件中,则这是一种方法。 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). 这种尝试是通过巧妙的load荷兰国际集团一切从对象的rda文件到一个新的environment (有很多教程和讨论使用和环境的相关指南,哈德利的“高级R”是一个做了很好的工作,我认为)。

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 . 使用否则为空的环境非常有用,这样我们可以使用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. 请注意,美元符号访问看起来与list相同,这使得(1)容易混淆两者,以及(2)易于理解$提供的命名索引。

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

This last piece, re-saving the rda file, is an indirect method. 最后一部分,重新​​保存rda文件,是一种间接方法。 The ls(envir = e) returns the variable names of all objects within the environment. ls(envir = e)返回环境中所有对象的变量名。 This is good, because save can deal with objects or with their names. 这很好,因为save可以处理对象或其名称。

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. 要知道,这在技术上并不追加 data.frame到rda文件,它已经结束了,写rda有一个新的,恰好包含所有以前的对象和新的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 . 默认情况下,它将覆盖现有对象,但可以使用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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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