[R] zero index and lazy evaluation in ifelse()

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jan 13 22:24:31 CET 2005


White.Denis at epamail.epa.gov writes:

> I don't understand this behavior:
> 
> > a <- c(0, 1, 2, 3)
> > b <- c(1, 2, 3, 4)
> > ifelse (a == 0, 0, b[a])
> [1] 0 2 3 1
> 
> rather than the desired 0 1 2 3.  Thanks for any explanation.

b[a] is c(1,2,3), recycling to length 4 gives c(1,2,3,1), get it?

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list