[R] how to get values within a threshold
    Zhang Weiwu 
    zhangweiwu at realss.com
       
    Fri Sep 13 17:52:20 CEST 2013
    
    
  
On Fri, 13 Sep 2013, William Dunlap wrote:
>> findInterval(thresholds, values)
> [1] 1 4 4 4 7
Thanks a lot! But now I have a new problem, a typical R issue perhaps.
First, let's look at  a successful case:
 	> thresholds <- c(1,3,5,7,9)
 	> values <- c(0.854, 1.648, 1.829, 1.874, 7.670, 7.673, 7.722)
 	> values[findInterval(thresholds, values)]
 	[1] 0.854 1.874 1.874 1.874 7.722
Then a new batch of values came, notice only the first element of new values 
differ:
 	> thresholds <- c(1,3,5,7,9)
 	> values <- c(1.254, 1.648, 1.829, 1.874, 7.670, 7.673, 7.722)
 	> findInterval(thresholds, values)
 	[1] 0 4 4 4 7
 	> values[findInterval(thresholds, values)]
 	[1] 1.874 1.874 1.874 7.722
This is a surprise. The desirable output is:
 	[1] 0     1.874 1.874 1.874 7.722
This is desirable, because so maintains the same number of elements during 
calculation. (You may suggest leaving out the indices and try to calculate 
maximum-values-below-threshold directly, but the indices are useful to 
address other fields in the data frame whence values came.)
This problem can be simplified as following:
in R, we have:
 	> a <- 1:10
 	> a[c(1,3)]
 	[1] 1 3
 	> a[c(0,3)]
 	[1] 3
While I was hoping to get:
 	> a <- 1:10
 	> a[c(1,3)]
 	[1] 1 3
 	> a[c(0,3)]
 	[1] 0 3
The straightforward solution, is to shift the whole test values one 
position, so that the first value is always zero:
 	> values <- c(0, 1.254, 1.648, 1.829, 1.874, 7.670, 7.673, 7.722)
This solution, despite begetting a train of changes elsewhere in the code, 
is semantically wrong, since the first element of values should be the first 
value, now it is actually the 0-th value.
What would you do in the case?
    
    
More information about the R-help
mailing list