[R] x[c(1,2,1)] <- 1:3

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Oct 24 11:56:35 CEST 2003


On Fri, 24 Oct 2003, Robin Hankin wrote:

> Hi everyone.
> 
> look at this:
> 
> x <- 1:4
> x[c(1,2,1)] <- (1:3)
> print(x[1])
> 
> I get 3, but isn't NA more appropriate? [1 would be as sensible].
> FWIW, the equivalent Fortran 95 statement is flagged as an error.
> R-intro, section 2.7, says that in such cases the assignment is
> carried out "in order" which might support getting 3.
> 
> To my way of thinking, the concept of "in order" seems to violate the
> usual strategy of considering vectors as whole entities---because in
> this case we have to specify whether the assignment starts at
> c(1,2,1)[1] and proceeds to c(1,2,1)[3], or starts at c(1,2,1)[3] and
> proceeds to c(1,2,1)[1].  And the results are different!
> 
> 
> What is the R position on this?

That this is correct and works as documented!  It is occasionally useful.

R implements the S language, not Fortran 95, so consult the S references 
please.

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