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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With