[R] how to get values within a threshold
William Dunlap
wdunlap at tibco.com
Fri Sep 13 18:04:51 CEST 2013
You may want to append -Inf (or 0 if you know the data cannot be
negative) to the start of your 'values' vector so you don't
have to write code to catch the cases when a threshold is below
the range of the values.
> findInterval(thresholds, c(0,values,Inf))
[1] 1 5 5 5 8
> c(0, values, Inf)[.Last.value]
[1] 0.000 1.874 1.874 1.874 7.722
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: Zhang Weiwu [mailto:zhangweiwu at realss.com]
> Sent: Friday, September 13, 2013 8:52 AM
> To: William Dunlap; smartpink111 at yahoo.com
> Cc: r-help at r-project.org
> Subject: RE: [R] how to get values within a threshold
>
>
>
> 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