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

White.Denis@epamail.epa.gov White.Denis at epamail.epa.gov
Thu Jan 13 22:39:08 CET 2005


This seems to contradict the help file.

"... 'yes' will be evaluated if and only if any element of 'test' is
true,
and analogously for 'no'..."


Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote on 2005-01-13 13:24:31:

> 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