Re: [R] renaming objects

From: Nordlund, Dan (DSHS/RDA) <NordlDJ_at_dshs.wa.gov>
Date: Mon, 03 Mar 2008 17:01:27 -0800

> -----Original Message-----
> From: r-help-bounces_at_r-project.org
> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Rolf Turner
> Sent: Monday, March 03, 2008 4:37 PM
> To: hadley wickham
> Cc: r-help; Giles.Crane_at_doh.state.nj.us
> Subject: Re: [R] renaming objects
>
>
> On 4/03/2008, at 11:48 AM, hadley wickham wrote:
>
> > On Mon, Mar 3, 2008 at 4:37 PM, Rolf Turner
> > <r.turner_at_auckland.ac.nz> wrote:
> >>
> >> 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.
> >
> > It doesn't though!
> >
> >> gc()
> > used (Mb) gc trigger (Mb) max used (Mb)
> > Ncells 133095 3.6 350000 9.4 350000 9.4
> > Vcells 87049 0.7 786432 6.0 478831 3.7
> >> a <- runif(1e7)
> >> gc()
> > used (Mb) gc trigger (Mb) max used (Mb)
> > Ncells 133112 3.6 350000 9.4 350000 9.4
> > Vcells 10087364 77.0 11458389 87.5 10087374 77.0
> >> b <- a
> >> gc()
> > used (Mb) gc trigger (Mb) max used (Mb)
> > Ncells 133117 3.6 350000 9.4 350000 9.4
> > Vcells 10087365 77.0 12111308 92.5 10087476 77.0
> >
> > R will only create a copy if either of a or b is modified.
>

> Well RMIHATMTTL!!! R is even cleverer than I thought!!!
>
> cheers,
>
> Rolf

But, the OP should know that in the above scenario, if a or b is changed the copy will be created, doubling the storage requirements. Of course, this can be prevented by removing vector a after the assignment.

Hope this is helpful,

Dan

Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services Olympia, WA 98504-5204    



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 Tue 04 Mar 2008 - 01:04:32 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 Tue 04 Mar 2008 - 08:30:19 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