-1

I have a daily time series of wind speeds and I would like R to return the average and maximum length of consecutive days beneath a certain threshold.

Example (Threshold = 2 m/s):

df > 
date              wind_speed
1970-01-01        1
1970-01-02        1
1970-01-03        3
1970-01-04        1
1970-01-05        1
1970-01-06        1
1970-01-07        3
1970-01-08        1

So. In this example the maximum length of consecutive days under 2 m/s would be "3" and the average lenght would be "2". Is there a way to do this in R for large data sets?

Thanks in advance

ChrM
  • 61
  • 3
  • 12
  • Maybe use max and mean on subsetted data? `max(df[ df$wind_speed > Threshold, "wind_speed"])` and `mean(df[ df$wind_speed > Threshold, "wind_speed"])` – zx8754 May 11 '16 at 09:19

1 Answers1

0

Untested

library(dplyr)
treshold <- 2
df %>%
  filter(wind_speed < treshold) %>%
  arrange(date) %>%
  mutate(
    break = date - lag(date) > 1,
    series = cumsum(break)
  ) %>%
  group_by(series) %>%
  summarise(
    length = n(),
  )
Thierry
  • 18,049
  • 5
  • 48
  • 66