[Rd] ifelse behaviour

Tony Plate tplate at acm.org
Thu Apr 26 16:07:26 CEST 2007


A simpler version of your "puzzling call to ifelse" is

ifelse(FALSE, character(0), integer(0))

The most obvious way to satisfy the requirements stated in the 
documentation is to extend integer(0) to length 1 by creating an NA 
value, and that's what you get as a return value (here the 'test' 
argument has length 1, and the 'no' argument has length 0).

-- Tony Plate


ml-it-r-devel at epigenomics.com wrote:
> Hi!
>
> I'm puzzled by the return value of ifelse
>
> consider
>
> x<-integer(0)
> ifelse(is(x, "character"), paste(x), x)
> [1] NA
>
> whereas
> if (is(x, "character")) return(paste(x)) else x
> [1] integer(0)
>
> or
> x<-integer(1)
> ifelse(is(x, "character"), paste(x), x)
> [1] 0
>
> work as I had anticipated. Is this correct behaviour?
>
> Regards,
>
>    Matthias
>
>
>  >sessionInfo()
> R version 2.5.0 (2007-04-23)
> i686-pc-linux-gnu
>
> locale:
> C
>
> attached base packages:
> [1] "stats"     "graphics"  "grDevices" "datasets"  "utils"     "methods"
> [7] "base"
>
> other attached packages:
> rcompletion    rcompgen
>      "0.1-2"    "0.1-10"
>
>
>



More information about the R-devel mailing list