From tplate at acm.org  Thu Apr 26 16:07:26 2007
From: tplate at acm.org (Tony Plate)
Date: Thu, 26 Apr 2007 08:07:26 -0600
Subject: [Rd] ifelse behaviour
In-Reply-To: <f0qasl$j79$1@perl.epigenomics.epi>
References: <f0qasl$j79$1@perl.epigenomics.epi>
Message-ID: <4630B21E.9010801@acm.org>

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"
>
>
>



[Rd] ifelse behaviour
Tony Plate