[R] using assign on save?

abunn@mymail.msu.montana.edu abunn at mymail.msu.montana.edu
Thu Sep 25 00:06:55 CEST 2003

I have an object which I want to save into an external file, using save, on a time step in loop. In the loop I have an if statment that will save when the appropraite time, t, is reached.

I want to change the object and the name of the saved object to reflect which time step is being written. I can do the file name in save using paste but cannot rename the object. 

For example, I want to save object foo at time t = 50 in file "Run.50.Rdata." Furthermore, I want the object in that file to be "foo50."

This is what I'm doing now:

    save(foo, file = paste("Run", t, "Rdata",sep="."))

which creates the right file name but obviously doesn't rename the object.

So, I tried assigning the object in the save function.

  save(assign(paste("foo", t, sep = ""), foo),
       file = paste("Run", t, "Rdata",sep="."))

This crashes. It would also leave a copy of foo50 in the workspace which is messy. 

The logic of how to rename the object at a time step, reference it in the save statement and then remove it is eluding me.

Any thoughts on how to proceed?


More information about the R-help mailing list