Find all numbers in a certain interval
It's not entirely clear what you are asking for, since
which(within.interval(a, -0.5, 0.5)) is actually longer than which(a >
-0.5 & a < 0.5). You mention that you want a solution that applies to
dataframes. Using indexing you can get entire rows of dataframes that
satisfy multiple conditions on one of its columns:
>> DF <- data.frame(a = rnorm(20), b= LETTERS[1:20], c =
letters[20:1], stringsAsFactors=FALSE)
> DF[which( DF$a > -0.5 & DF$a < 0.5 ), ]
# note that one needs to avoid DF[which(a > -0.5 & a<0.5) , ]
# the "a" vector is not the same as the "a" column vector within DF
a b c
3 -0.47310672 C r
6 -0.49784460 F o
9 0.02571058 I l
10 0.16893759 J k
11 -0.11963322 K j
12 0.39378887 L i
16 0.03712263 P e
Could get the indices that satisfy more than one condition:
> which(DF$a > 0.5 & DF$b < "K")
[1] 1 2 6 10
Or you can get rows of DF that satisfy conditions on multiple columns
with the subset function:
> subset(DF, a > 0.5 & b < "K")
a b c
1 2.2500997 A t
2 0.7251357 B s
6 0.7845355 F o
10 1.0685649 J k
Or if you wanted a within.interval function
> within.interval <- function(x,a,b) { x > a & x < b}
> which(within.interval(DF$a, -0.5, 0.5))
[1] 3 4 7 8 9 13 14 17 20
David Winsemius Heritage Labs On Dec 16, 2008, at 5:09 AM, 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? > > > Antje > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.