title: Separate operations on groups of time series values identified by same flag in R
body: <p>Does anyone have a solution to perform</p>
  
  <ul>
  <li>separate operations on</li>
  <li>groups of consecutive values that are a</li>
  <li>subset of a time series and are</li>
  <li>identified by a reoccurring, identical flag</li>
  <li>with R ?</li>
  </ul>
  
  <p>In the example data set created by the code below, this would refer for example to calculating the mean of “value” separately for each group where “flag” == 1 on consecutive days.</p>
  
  <p>A typical case in science would be a data set recorded by an instrument that repeatedly executes a calibration procedure and flags the corresponding data with the same flag, but the user needs to evaluate each calibration separately with the same procedure.</p>
  
  <p>Thanks for your suggestions. Jens</p>
  
  <pre><code>library(lubridate)
  
  df &lt;- data.frame(
      date = seq(ymd("2018-01-01"), ymd("2018-06-29"), by = "days"),
      flag = rep( c(rep(1,10), rep(0, 20)), 6),
      value = seq(1,180,1)
    )
  </code></pre>