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")
There are numerous examples of renaming
variables (columns) in dataframes, but I have
found no methods for renaming objects.
The functionassign() does not do this.
Thank you for any help you can give.
Cordially
Giles Crane, M.Phil., MPH
Research Scientist & Statistician
New Jersey Department of Health & Senior Services
Tel 609 292-8012, -5666
Fax 609 292-9288
giles.crane at doh.state.nj.us
renaming objects
10 messages · Giles.Crane at doh.state.nj.us, Eric Fail, Rolf Turner +6 more
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. Ericka
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
######################################################################
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
This does not really answer your question but the following lets you refer to an object by a second name although the object still has its original name as well.
a <- 3
makeActiveBinding("b", function() a, .GlobalEnv)
NULL
b
[1] 3
a <- 4 b
[1] 4
On Mon, Mar 3, 2008 at 4:20 PM, <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")
There are numerous examples of renaming
variables (columns) in dataframes, but I have
found no methods for renaming objects.
The functionassign() does not do this.
Thank you for any help you can give.
Cordially
Giles Crane, M.Phil., MPH
Research Scientist & Statistician
New Jersey Department of Health & Senior Services
Tel 609 292-8012, -5666
Fax 609 292-9288
giles.crane at doh.state.nj.us
______________________________________________ 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.
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.
or, in other terms: > set.seed(1) > x <- runif(1e7) > x.add <- tracemem(x) > y <- x > y.add <- tracemem(y) > identical(x.add, y.add) [1] TRUE ie, both objects have the same memory address - therefore, not a copy: > x.add [1] "<0x2000000>" > y.add [1] "<0x2000000>" now, observe what happens when you modify "y" > y[1] <- 1 tracemem[0x2000000 -> 0x6c4c000]: > tracemem(y) [1] "<0x6c4c000>" now, it's a copy... =) b
On Mar 3, 2008, at 5:48 PM, 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. 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.
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 ###################################################################### 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 ######################################################################
-----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
On Mon, 3 Mar 2008, Nordlund, Dan (DSHS/RDA) wrote:
[..., quoting Hadley Wickham]
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.
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.
Hadley was correct: it is not prevented by removing 'a', as R does not
have reference counting. E.g.
rm(a)
b[1] <- 1
gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 133947 3.6 350000 9.4 350000 9.4
Vcells 10087573 77.0 21337085 162.8 20087562 153.3
Note the 'max used' Vcells.
There's a fairly complete explanation of what happens in the 'R Internals'
manual.
I think the most common source of confusion is over the term 'objects'. R
does not have 'objects' in this sense: 'a' and 'b' are symbols with
bindings to values. So you cannot change 'b', but you can change its
binding. When you do b[1] <- 1 you may create a new C-level structure as
the new value, or you may change the existing one. In this case it
created a new structure (by copying the old one and altering that).
Certain replacement functions are the only way to avoid making a new
value: a <- a+0 for example always creates a new value (at a different
address in memory) even though its contents will be identical.
Once we get away from the simplest vectors more sharing can be done: e.g.
character vectors with duplicate elements will share storage for those
elements.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595