[R] Find all numbers in a certain interval
Duncan Murdoch
murdoch at stats.uwo.ca
Tue Dec 16 12:58:36 CET 2008
Antje wrote:
> Hi all,
>
> I'd like to know, if I can solve this with a shorter command:
>
> a <- rnorm(100)
> which(a > -0.5 & a < 0.5)
>
> # would give me all indices of numbers greater than -0.5 and smaller than +0.5
>
> I have something similar with a dataframe and it produces sometimes quite long
> commands...
> I'd like to have something like:
>
> which(within.interval(a, -0.5, 0.5))
>
> Is there anything I could use for this purpose?
Not in general, but in this particular case "abs(a) < 0.5" gives you the
right result.
By the way, some advice I read many years ago (in Kernighan and
Plauger): always use < or <=, avoid > or >= in multiple comparisons.
It's easier to read
-0.5 < a & a < 0.5
than it is to read the form you used, because it is so much like the
math notation -0.5 < a < 0.5.
Duncan Murdoch
More information about the R-help
mailing list