[R] renaming objects

Ravi Varadhan rvaradhan at jhmi.edu
Tue Mar 4 00:09:12 CET 2008


This is interesting!

> gc()
         used (Mb) gc trigger  (Mb) max used  (Mb)
Ncells 151051  4.1     350000   9.4   350000   9.4
Vcells  94969  0.8   20380703 155.5 30094956 229.7

> a <- runif(1e07)
> gc()
           used (Mb) gc trigger  (Mb) max used  (Mb)
Ncells   151046  4.1     350000   9.4   350000   9.4
Vcells 10094946 77.1   20380703 155.5 30094956 229.7

> b <- a 
> gc()
           used (Mb) gc trigger  (Mb) max used  (Mb)
Ncells   151047  4.1     350000   9.4   350000   9.4
Vcells 10094946 77.1   20380703 155.5 30094956 229.7

> b <- a + 0
> gc()
           used  (Mb) gc trigger  (Mb) max used  (Mb)
Ncells   151048   4.1     350000   9.4   350000   9.4
Vcells 20094946 153.4   21479738 163.9 30094956 229.7
>

In the last case, R makes an extra copy of "a", because it "thinks" that it
has been modified ???

Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html



----------------------------------------------------------------------------
--------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of hadley wickham
Sent: Monday, March 03, 2008 5:48 PM
To: Rolf Turner
Cc: r-help; Giles.Crane at doh.state.nj.us
Subject: Re: [R] renaming objects

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.

Hadley



-- 
http://had.co.nz/

______________________________________________
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.



More information about the R-help mailing list