[R] Strange results using match and which function

Sarah Goslee sarah.goslee at gmail.com
Mon Nov 4 19:52:58 CET 2013


This looks like R FAQ 7.31, the one so commonly asked that regulars on
this list have its number memorized!

>  tau<-seq(0.02,0.98,0.02)
> match(round(0.12, 2), round(tau, 2))
[1] 6
> match(round(0.16, 2), round(tau, 2))
[1] 8

See also ?all.equal

Sarah

On Mon, Nov 4, 2013 at 1:29 PM, Timo Schmid <timo_schmid at hotmail.com> wrote:
> Hello,
> I want to match specific numbers and a vector. Therefore I use the match or which function but I get unreasonable results. Has anybody an idea why I got a NA inmatch(0.12, tau)Please see the code below:
>
>> tau<-seq(0.02,0.98,0.02)
>> tau
>  [1] 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40
> [21] 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80
> [41] 0.82 0.84 0.86 0.88 0.90 0.92 0.94 0.96 0.98
>> match(0.02, tau)
> [1] 1
>> match(0.12, tau)
> [1] NA
>> match(0.16, tau)
> [1] 8
>>
>> which(0.12==tau)
> integer(0)
>> which(0.16==tau)
> [1] 8
>>
>> 0.14%in%tau
> [1] FALSE
>> 0.16%in%tau
> [1] TRUE
>
>
>
> Thanks in advance
> Timo
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list