Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

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

Tal Galili

2010-09-01

Replies: Find Java Web Hosting

Author LoginPost Reply
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

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@(protected)
Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
www.r-statistics.com (English)
----------------------------------------------------------------------------------------------

 [[alternative HTML version deleted]]

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.