[R] ifelse

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Mon May 13 14:40:25 CEST 2002


See the NEWS in R 1.5.0 and 150update.txt on developer.r-project.org
about testing equality of possibly missing values.

aa$x[4]=="x" is incorrect usage (and always has been).  The result is now
logical NA, and ifelse(NA, "a", "b") returns NA as the answer is unknown.

Use %in% or identical().

On Mon, 13 May 2002, juli g. pausas wrote:

>
> I suppose this is a very basic question, but I cannot understant it:
>
> > aa <- data.frame(x = c(rep("x", 3), NA, NA, NA), y= 1:6)
> > aa$x[4]
> [1] <NA>
> Levels:  x
> > aa$y[4]
> [1] 4
> > aa$y[4] <- ifelse(aa$x[4]=="x", NA, aa$y[4])
> > aa$y[4]
> [1] NA
> >
>
> I was expecting aa$y[4] not to be changed, but I may missundertood
> something.
>
> aa2 <- data.frame(x = c(rep("x", 3), rep("b", 3)), y= 1:6)
> aa2$y <- ifelse(aa2$x=="x", NA, aa2$y)
>
> this works OK
>
> (R 1.5.0 on Windows)
>
> juli
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list