[R] Diffrerence in "%in%" function to boundry setting via <>
Roger Bivand
Roger.Bivand at nhh.no
Thu Feb 8 11:51:48 CET 2007
On Thu, 8 Feb 2007, Benjamin Otto wrote:
A version of FAQ 7.31 "Why doesn't R think these numbers are equal?"
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
> ps <- seq(-0.502,0.378,by=0.001)
> ps[494]
[1] -0.009
> print(ps[494], digits=16)
[1] -0.009000000000000008
> all.equal(ps[494], -0.009)
[1] TRUE
> Hi,
>
> There is a point which is irritating me currently quite a bit and that is an
> aspect of different behaviour between the %in% function and the
> smaller/bigger than signs (<>). Here is are two examples to demonstrate what
> I mean:
>
> Example1:
> > c(1,1,2,2,3,4,4,6,7) %in% c(1,2,3)
> [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
>
> Right, that is what I expect.
>
> Example2:
> > ps <- seq(-0.502,0.378,by=0.001)
> > ps[494]
> [1] -0.009
>
> > class(ps[494])
> [1] "numeric"
> > class(-0.009)
> [1] "numeric"
> > class(ps[494])
> [1] "numeric"
>
> > ps[494] == -0.009
> [1] FALSE
> > ps[494] %in% -0.009
> [1] FALSE
> > ps[494] == c(-0.009)
> [1] FALSE
> > ps[494] %in% c(-0.009)
> [1] FALSE
> > ps[494] <= -0.008
> [1] TRUE
> > ps[494] >= -0.010
> [1] TRUE
> > -0.009 == -0.009
> [1] TRUE
>
> BUT:
> > ps[249]
> [1] -0.254
> > class(ps[249])
> [1] "numeric"
> > ps[249] %in% -0.254
> [1] TRUE
>
> OK! Can sombody explain to me what is happening here? Honestly? I don't
> understand where the difference but it's critical! Because obviuosly when I
> have a set of numeric values (ALL have three digits) and to boundry values
> lb/up, a lower and an upper boundry, I could (from what I thought until now)
> chosse between:
>
> Version1:
> > small.set <- set[set %in% seq(lb,up,by=0.001)]
>
> Version2:
> > small.set <- set[set >= lb & set <= up]
>
> Unfortunately with my data I used I got around 8000 values from my set with
> version1 but about 24000 with version2. IS there some main diffrence I
> didn't take into account or is my system just behaving irrational (that's
> what I think if you look at Example2)?
>
> I checked the behaviour under R-2.4.1 (Windows) and under 2.2.1 (Linux). The
> result was the same.
>
> Sincere regards
>
> Benjamin Otto
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-help
mailing list