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

From: <wwl107_at_psu.edu>
Date: Wed 22 Jun 2005 - 02:46:49 GMT

I did the following ('g' and 'h' are both numeric vectors)
> i <- abs(g-h)

creating a vector 'i' with values,
> i

[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 0.00 0.13

Now, I want to create a new vector =1 whenever 'i' = 0.33 and =0 otherwise. I use the 'ifelse' function to do this but when I do, I find
> ifelse(i==0.33,1,0)

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

even though the third and eighth elements of 'i' clearly equal 0.33. Unless I have missed something, 'ifelse' should return a vector that looks like, [1] 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0

When I try 0.13 I get a correct result
> ifelse(i==0.13,1,0)

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

but when I try 0.08 or 0.2 I find a similar problem,
> ifelse(i==0.08,1,0)

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> ifelse(i==0.20,1,0)

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> ifelse(i==0.2,1,0)

[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Can you explain what is happening? Why is R incorretly returning a vector that does not reflect the values in 'i'? Is this due to a malfunction in R or have I missed something?