[R] Find all numbers in a certain interval
David Winsemius
dwinsemius at comcast.net
Tue Dec 16 13:41:26 CET 2008
On Dec 16, 2008, at 7:19 AM, Antje wrote:
> 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 ), ]
I see your point, but I must point out that no cases would ever
satisfy that construction.
>
>
> instead of:
>
> DF[which( within.interval(DF$myReallyLongColumnName,
> myReallyLongThreshold), ]
That would be a different within.interval function than I suggested,
but you could certainly create one which accepted a vector.
within.interval <- function(x, y) { min(y) < x & x < max(y) }
----------
> within.interval2 <- function(x,y) { min(y) < x & x < max(y)}
> y <- c(-.1, -.2, .1,.2)
> which(within.interval2(DF$a,y))
[1] 7 13 14 17
>
>
> 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
>>
>
> ______________________________________________
> 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.
More information about the R-help
mailing list