繁体   English   中英

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

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

我是R的新手,将尽我所能使自己理解。 假设我有一个包含多个对象的现有rdata文件。 现在我要向其中添加一个数据框,我该怎么做? 我尝试了以下方法:

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=""))

但这似乎行不通吗?

我不确定您的实际用例,但是如果必须将新对象“附加”到rda文件中,则这是一种方法。 这种尝试是通过巧妙的load荷兰国际集团一切从对象的rda文件到一个新的environment (有很多教程和讨论使用和环境的相关指南,哈德利的“高级R”是一个做了很好的工作,我认为)。

第一步将所有对象加载到新的(空)环境中。 使用否则为空的环境非常有用,这样我们可以使用ls轻松地从中获取所有对象。

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

您要添加的对象应加载到环境中的变量中。 请注意,美元符号访问看起来与list相同,这使得(1)容易混淆两者,以及(2)易于理解$提供的命名索引。

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

最后一部分,重新​​保存rda文件,是一种间接方法。 ls(envir = e)返回环境中所有对象的变量名。 这很好,因为save可以处理对象或其名称。

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

要知道,这在技术上并不追加 data.frame到rda文件,它已经结束了,写rda有一个新的,恰好包含所有以前的对象和新的data.frame文件。

我写了这个解决方案,可以添加数据框,列表,矩阵或列表。 默认情况下,它将覆盖现有对象,但可以使用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