[R] renaming objects
Rolf Turner
r.turner at auckland.ac.nz
Mon Mar 3 23:37:43 CET 2008
On 4/03/2008, at 10:38 AM, Ericka Lundström wrote:
> On 03/03/2008, at 22.20, Giles.Crane at doh.state.nj.us wrote:
>> Is there a way to rename R objects?
>> I am looking for a way to rename objects
>> without making new objects.
>>
>> #For example:
>> x = c(1:40)
>> # I wish to use a function to rename x, already created, to y,
>> perhaps by
>> obj.rename(x,y)
>> # or
>> obj.rename("x","y")
> y <- x # changes x to y with same values.
That makes a new object --- which is precisely what the enquirer
wanted ***NOT*** to do.
That being said, I'm pretty sure that the answer to the original
question ``Is there a way ... ?'' is ``No.'' The R-Core people
will be able to provide a definitive answer, but it appears to me
that the way a name is associated with an object is buried deep
in the innards of R and is not accessible to the user.
At this stage, I'd like to enquire ***why*** is it desired to
be able to do this? The one rationale that I can think of is the
situation that x is so huge that it is impossible to make a copy
of it (via ``y <- x'') without exceeding R's memory capacity.
If x is not that huge, one can simply do ``y <- x'' and then
``rm(x)'' and presto one has changed the object's name from ``x''
to ``y''.
I have written my own little utility function ``mv()'', imitating
the Unix command ``mv'' to facilitate this:
mv <- function (a, b) {
anm <- deparse(substitute(a))
bnm <- deparse(substitute(b))
if (!exists(anm,where=1,inherits=FALSE))
stop(paste(anm, "does not exist.\n"))
if (exists(bnm,where=1,inherits=FALSE)) {
ans <- readline(paste("Overwrite ", bnm, "? (y/n) ", sep =
""))
if (ans != "y")
return(invisible())
}
assign(bnm, a, pos = 1)
rm(list = anm, pos = 1)
invisible()
}
######################################################################
Attention:
This e-mail message is privileged and confidential. If you are not the
intended recipient please delete the message and notify the sender.
Any views or opinions presented are solely those of the author.
This e-mail has been scanned and cleared by MailMarshal
www.marshalsoftware.com
######################################################################
More information about the R-help
mailing list