3

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.

Curious2learn
  • 31,692
  • 43
  • 108
  • 125
  • I've flagged your message as better fitting for stackoverflow. I think the moderators will move it there pretty soon, so there is no need to create a new question there. – Nick Sabbe Jun 11 '11 at 13:58

4 Answers4

8

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
4

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 )
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • This also answered my question on writing to CSV too! http://stackoverflow.com/q/32398127/224707 – Nick Sep 04 '15 at 12:33
1

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.

mbq
  • 18,510
  • 6
  • 49
  • 72
  • @hadley The idea is in stacking many objects in one file, `*RDS` can only put/get one. – mbq Jun 15 '11 at 07:02
0

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
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
  • I know it is present in the same variable. However, suppose I forget what the variable was. 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! – Curious2learn Jun 11 '11 at 14:24