Re: [R] renaming objects

From: Rolf Turner <r.turner_at_auckland.ac.nz>
Date: Tue, 04 Mar 2008 11:37:43 +1300

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

######################################################################

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 03 Mar 2008 - 22:48:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Mar 2008 - 23:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive