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

White.Denis@epamail.epa.gov White.Denis at epamail.epa.gov
Thu Jan 13 23:44:24 CET 2005


Thanks to all for clarifying that it isn't an element by element test
and evaluation, rather an "any" test as the code says.


Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote on 2005-01-13 14:22:50:

> White.Denis at epamail.epa.gov writes:
>
> > 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'..."
> >
>
> It doesn't.  Read closer.  Both 'yes' and 'no' is evaluated. The
> former is c(0, 0, 0, 0) the latter is c(1, 2, 3, 1).
>
>
> > 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?
> > >




More information about the R-help mailing list