[R] "match" and "which" give NA for some values, but not others
Rolf Turner
rolf.turner at xtra.co.nz
Wed Jul 27 09:42:23 CEST 2011
You are failing to take account of floating point arithmetic.
This is FAQ 7.31.
Really, it is advisable to check the FAQ before firing off a
naive inquiry to the r-help list.
cheers,
Rolf Turner
On 27/07/11 15:50, Heemun Kwok wrote:
> Hello list,
> I am having trouble with "match" and "which" giving a NA result for some values, but not others. Here is a simple example:
>
>> aaa<- seq(0,1,by=0.05)
>> aaa
> [1] 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70
> [16] 0.75 0.80 0.85 0.90 0.95 1.00
>
>> match(0.5,aaa)
> [1] 11
>
>> match(0.6,aaa)
> [1] NA
>
>> which(aaa==0.5)
> [1] 11
>
>> which(aaa==0.6)
> integer(0)
>
>> sessionInfo()
> R version 2.12.2 (2011-02-25)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> Am I missing something? Any thoughts?
More information about the R-help
mailing list