Reputation: 33648
Suppose I have an .rda
file created using save()
. Example:
save(mydata1, file = "anrdatafile.rda")
where, mydata1
is a data frame.
I want to write code in R
which: (1) loads the 'anrdatafile.rda' file; (2) finds the name of the data frame in that file (3) combines the found data frame with another data frame say mydata2
.
How do I achieve step 2? Once I have the name of the data frame in step 2, I can do
combineddata = rbind(mydata1, mydata2)
But I don't know how to get that the name of the dataframe in the .rda file is mydata1
as a part of a code.
I tried
nameofthedataframe = load('anrdatafile.rda')
This assigns the string "mydata1" to the variable "nameofthedataframe", but then how do I get the data in the data frame mydata1
?
To clarify, I know I can use the same data frame name that was to used to save the data. However, suppose I forget what the variable was. Or, more importantly, I would have to hard code the variable name in my code. I was wondering whether the program could figure out the name of the data frame during the run time once I gave it the file name.
Thanks. If it is not clear, please let me know. I will try to clarify.
Upvotes: 3
Views: 5822
Reputation: 368647
To get the name of one or more variables in a RData
file, use an environment to load into.
First, create and save some data:
R> grumpy <- 1; happy <- 42; sneezy <- 1/7
R> save(grumpy, happy, sneezy, file="/tmp/Dwarves.RData")
R>
Then in a new (or cleaned) R session:
R> ls()
character(0)
R> myenv <- new.env()
R> load("/tmp/Dwarves.RData", env=myenv)
R> ls(envir=myenv)
[1] "grumpy" "happy" "sneezy"
R>
R> myenv$sneezy
[1] 0.142857
R> myenv$happy
[1] 42
R>
and you see that the variables in that environment supplied to load()
correspond to what we save in the file.
Upvotes: 8
Reputation: 18638
If you accidentally want to first make a lot of RData files with single data frame and then merge them, I think the rtape
package will do the job in a more comfy way. It allows you to make a sort of appendable RData files and then convert them into a list or iterate over them.
Anyway, in your case, it would look more-less like this:
#Make a new data frame, say A
rtapeAdd('myTape.tape',A) #This will create myTape.tape file
#...
#Make even newer data frame, say B
rtapeAdd('myTape.tape',B)
#...
#...
rtapeAdd('myTape.tape',Z)
#Now the merge
do.call(rbind,rtapeAsList('myTape.tape'))->mergedDataFrame
As you can see, rtape is not using any names for single entries (only order matters) so you don't have a problem of browsing them.
Upvotes: 1
Reputation: 49680
If you have the name of an object, but you want the actual value, then use the get
function. So you can do something like:
combineddata <- rbind( get(nameofthedataframe), mydata2 )
Upvotes: 4
Reputation: 11956
I'll answer it anyway:
Once you've run the load command, the dataframe is once again present in the same variable! You can easily test this type of thing with:
a<-1:5
a #[1] 1 2 3 4 5
save(a, "test.txt")
rm(a) #remove a from environment
a #Error: object 'a' not found #elvis has left the building!
namesloaded<-load("test.txt") #after this, namesloaded contains "a"
a #[1] 1 2 3 4 5 #elvis just rose from the dead
Upvotes: 0