From maechler at stat.math.ethz.ch  Wed Jun 22 10:06:34 2005
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Wed, 22 Jun 2005 10:06:34 +0200
Subject: [Rd] Trouble with ifelse and if statement (PR#7962)
In-Reply-To: <20050622033318.29D25A22C@slim.kubism.ku.dk>
References: <20050622033318.29D25A22C@slim.kubism.ku.dk>
Message-ID: <17081.7178.640030.851104@stat.math.ethz.ch>

       
       <........nonsense non-bug report by Woolton........>

    Marc> This is not a bug and yes you have missed something.

    Marc> Read R FAQ 7.31 Why doesn't R think these numbers are equal?

    Marc> More information is also available here:

    Marc> http://grouper.ieee.org/groups/754/

thank you, Marc.

    Marc> One possible solution:

    >> i
    Marc> [1] 0.08 0.00 0.33 0.00 0.00 0.00 0.00 0.33 0.00 0.00 0.08 0.08 0.20
    Marc> [14] 0.00 0.13

Note that a slightly more recommended way for the following is

  as.integer(sapply(i, function(x) isTRUE(all.equal(x, 0.33))))

using the    isTRUE(all.equal(...))  idiom 
which I'd recommend quite generally.

Martin

    >> ifelse(sapply(i, function(x) all.equal(x, 0.33)) == "TRUE", 1, 0)
    Marc> [1] 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0

    >> ifelse(sapply(i, function(x) all.equal(x, 0.08)) == "TRUE", 1, 0)
    Marc> [1] 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0

    >> ifelse(sapply(i, function(x) all.equal(x, 0.2)) == "TRUE", 1, 0)
    Marc> [1] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

Martin



[Rd] Trouble with ifelse and if statement (PR#7962)
Martin Maechler