[R] Find all numbers in a certain interval
Antje
niederlein-rstat at yahoo.de
Tue Dec 16 13:19:35 CET 2008
Hi David,
thanks a lot for your proposal. I got a lot of useful hints from all of you :-)
David Winsemius schrieb:
>
> 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).
Right but in case 'a' is something with a long name and '0.5' is a variable you
might end up with something like this (for the data frame example):
DF[which( DF$myReallyLongColumnName > -myReallyLongThreshold &
DF$myReallyLongColumnName < -myReallyLongThreshold ), ]
instead of:
DF[which( within.interval(DF$myReallyLongColumnName, myReallyLongThreshold), ]
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
>
>
>
More information about the R-help
mailing list