[R] Overlapping assignment

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 25 08:31:01 CEST 2006


On Mon, 24 Apr 2006, Rob Steele wrote:

> Is it valid to assign a slice of an array or vector to an overlapping
> slice of the same object?  R seems to do the right thing but I can't
> find anything where it promises to.

Yes.  Remember R is a vector language, so it is not doing this 
index-by-index.  Also, what you are doing is

a <- `[<-`(a, 4:12, 1:9)

which should make this easier to understand: you get a new object, and you 
don't expect

x <- x^2

to be a problem, do you?

>
> > a <- 1:12
> > a[4:12] <- a[1:9]
> > a
>  [1] 1 2 3 1 2 3 4 5 6 7 8 9
>
> > b <- 1:12
> > b[1:9] <- b[4:12]
> > b
>  [1]  4  5  6  7  8  9 10 11 12 10 11 12
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

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




More information about the R-help mailing list