[R] Why is vector assignment in R recreates the entire vector ?

Bert Gunter gunter.berton at gene.com
Wed Sep 1 17:35:51 CEST 2010


On Wed, Sep 1, 2010 at 8:09 AM, Tal Galili <tal.galili at gmail.com> wrote:
> Hello all,
>
> A friend recently brought to my attention that vector assignment actually
> recreates the entire vector on which the assignment is performed.
>
> So for example, the code:
> x[10]<- NA # The original call (short version)
>
> Is really doing this:
> x<- replace(x, list=10, values=NA) # The original call (long version)
> # assigning a whole new vector to x
This has been much discussed on this list. Short answer: R is a
functional programming lanugage that uses call by value, not
references.

Longer answer: It depends. R will not create a copy if it can avoid it
(usually?). Search the list archives for "call by value", "copy
arguments", etc. for authoritative answers.

-- 
Bert Gunter
Genentech Nonclinical Statistics

> Which is actually doing this:
> x<- `[<-`(x, list=10, values=NA) # The actual call
>
>
> Assuming this can be explained reasonably to the lay man, my question is,
> why is it done this way ?
> Why won't it just change the relevant pointer in memory?
>
> On small vectors it makes no difference.
> But on big vectors this might be (so I suspect) costly (in terms of time).
>
>
> I'm curious for your responses on the subject.
>
> Best,
> Tal
>
>
>
> ----------------Contact
> Details:-------------------------------------------------------
> Contact me: Tal.Galili at gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> ----------------------------------------------------------------------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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