# [R] selecting consecutive records over a threshold

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 27 19:22:35 CET 2008

Convert the data frame to a zoo object and note that:

diff(-rollmax(-z, 2) > 2) > 0
diff(rollmax(z, 2) < 1) > 0

have 1 at the start and end of the storm period respectively
so that cumsum of their difference has ones for the storm period.
In the last line we extract that portion.

# input
DF <- data.frame(HourCount = 1:14,
Amt = c(0, 0, 0.3, 3, 4, 8, 10, 15, 12, 6, 4, 3, 0.2, 0.2))

library(zoo)
z <- with(DF, zoo(Amt, HourCount))

r <- cumsum((diff(-rollmax(-z, 2)  > 2) > 0) - (diff(rollmax(z, 2)  < 1) > 0))
window(z, time(r[r > 0]))

(You may have to use align="right" argument to both rollmax occurrences
depending on how you want to define a storm period.)

See ?rollmax and the three vignettes on the zoo package for more info.

On Wed, Feb 27, 2008 at 12:00 PM, Jamie Ledingham
<jamie.ledingham at newcastle.ac.uk> wrote:
>
> Dear all,
> I am having trouble working out how I might do the following and would
> appreciate any thoughts.
> I am working with data concerning precipitation.  The data are in 2
> columns in a data frame called "storm" in the following format:
>
> HourCount - 1,2,3,4,5,6,7,8,...48
> Amt - 0,0,0.3,3,4,8,10,15,12,6,4,3,0.2,0.2...
>
> There are 48 hours worth of data.  I am trying to extract a storm.  My
> storm is defined as a threshold - when the amount is greater than 2 for
> 2 hours the storm starts, and when the amount is less than 1 for two
> hours the storm ends.
> I can extract data above thresholds but it is obviously important to be
> able to extract consecutive records to capture the whole storm.
>
> Can anybody help?
> Thanks
> Jamie Ledingham
> PhD Researcher
> University of Newcastle Upon Tyne
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help