[R] real and complex vectors

Robin Hankin r.hankin at soc.soton.ac.uk
Wed Feb 16 11:00:01 CET 2005


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?


--
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743




More information about the R-help mailing list