[R] real and complex vectors

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Feb 16 11:19:37 CET 2005


On Wed, 16 Feb 2005, Robin Hankin wrote:

> The following caught me off-guard:
>
>
> R> z <- 1i + 1:10
> R> z <- Re(z)
> R> z
> [1]  1  2  3  4  5  6  7  8  9 10
>
> as expected.  But look:
>
> R> z <- 1i + 1:10
> R> make.real <- abs(z) < 1000
> R> z[make.real] <- Re(z[make.real])
> R> z
> [1]  1+0i  2+0i  3+0i  4+0i  5+0i  6+0i  7+0i  8+0i  9+0i 10+0i
> R>
>
> didn't make z a real vector, which is what I wanted.  ?"[<-" says
>
>     If one of these expressions appears on the left side of an
>     assignment then that part of 'x' is set to the value of the right
>     hand side of the assignment.
>
> so the behaviour is as documented: class(z) is unchanged in the second 
> session.
>
> Would modifying "[<-" to add a test for all elements of an object being 
> replaced (and if this is the case to change the class of z 
> appropriately), be a bad idea?

Yes.  Don't expect your interpreter to mind-read. Changing basic things 
like this is likely to break lots of existing code.

R-help is not really the place for programming design questions (R-devel 
is).

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