How can I change the name of a data frame

The truth is that objects in R don’t have names per-se. There exists different kinds of environments, including a global one for every process. These environments have lists of names, that point to various objects. Two different names can point to the same object. This is best explained to my knowledge in the environments chapter of Hadley Wickhams Advanced R book http://adv-r.had.co.nz/Environments.html

So there is no way to change a name of a data frame, because there is nothing to change.

But you can make a new name (like newname) point to the same object (in your case a data frame object) as an given name (like oldname) simply by doing:

   newname <- oldname

Note that if you change one of these variables a new copy will be made and the internal references will no longer be the same. This is due to R’s “Copy on modify” semantics. See this post for an explanation: What exactly is copy-on-modify semantics in R, and where is the canonical source?

Hope that helps. I know the pain. Dynamic and functional languages are different than static and procedural languages…

Of course it is possible to calculate a new name for a dataframe and register it in the environment with the assign command – and perhaps you are looking for this. However referring to it afterwards would be rather convoluted.

Example (assuming df is the dataframe in question):

   assign(  paste("city_stats", city_code, sep = ""), df )

As always see the help for assign for more information http://stat.ethz.ch/R-manual/R-devel/library/base/html/assign.html

Edit: In reply to your edit, and various comments around the problems with using eval(parse(...) you could parse the name like this:

head(get(gear_subset))

Leave a Comment