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" -- Matthias Burger Project Manager/ Biostatistician Epigenomics AG Kleine Praesidentenstr. 1 10178 Berlin, Germany phone:+49-30-24345-371 fax:+49-30-24345-555 http://www.epigenomics.com matthias.burger at epigenomics.com -- Epigenomics AG Berlin Amtsgericht Charlottenburg HRB 75861 Vorstand: Geert Nygaard (CEO/Vorsitzender), Dr. Kurt Berlin (CSO) Oliver Schacht PhD (CFO), Christian Piepenbrock (COO) Aufsichtsrat: Prof. Dr. Dr. hc. Rolf Krebs (Chairman/Vorsitzender)
On 4/26/2007 9:53 AM, 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] NAThe test evaluates to a length 1 logical vector containing FALSE. So ifelse() tries to return the first entry of x. But x[1] doesn't exist, so it evaluates to NA, and that's what ifelse() returns.> > whereas > if (is(x, "character")) return(paste(x)) else x > [1] integer(0)This is quite different. It says to return the expression in the else clause, which is x.> > or > x<-integer(1) > ifelse(is(x, "character"), paste(x), x) > [1] 0 > > work as I had anticipated. Is this correct behaviour?Yes, this is correct. Duncan Murdoch
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" > > >
Maybe Matching Threads
- R 2.5.0 devel try issue in conjuntion with S4 method dispatch
- getNamespaceExports("base") error
- R 2.9.0 devel: package installation with configure-args option
- R devel repository tarball naming issue
- R (2.9.0 rc) CMD INSTALL will leave OOLOCK folder even if installation succeeded