title: Keeping maximum and minimum values in a logical sequence
body: <p>Consider this data:</p>
  <pre><code>df &lt;- structure(list(Date = structure(c(2922, 4018, 5113, 7305, 8035, 
  12053, 14975, 16436, 17532, 17897), class = &quot;Date&quot;), HAM = c(1016.89391375364, 
  -1269.0910012255, -1097.9927692669, -5069.52785909119, 3168.39687262048, 
  -1265.24208195278, -1218.5560466457, 1463.67252927616, 1259.20509267793, 
  1267.89637533522), State = c(&quot;Expansion&quot;, &quot;Contraction&quot;, &quot;Contraction&quot;, 
  &quot;Contraction&quot;, &quot;Expansion&quot;, &quot;Contraction&quot;, &quot;Contraction&quot;, &quot;Expansion&quot;, 
  &quot;Contraction&quot;, &quot;Expansion&quot;), sd = c(&quot;larger&quot;, &quot;smaller&quot;, &quot;smaller&quot;, 
  &quot;smaller&quot;, &quot;larger&quot;, &quot;smaller&quot;, &quot;smaller&quot;, &quot;larger&quot;, &quot;larger&quot;, 
  &quot;larger&quot;)), row.names = c(NA, -10L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>I would like to keep a logical sequence of &quot;Expansion&quot; followed by &quot;Contraction&quot;, taking into consideration the maximum value (for Expansion) and minimum value (for &quot;Contraction&quot;). Expected result is this data frame:</p>
  <pre><code>ndf &lt;- structure(list(Date = structure(c(2922, 7305, 8035, 12053, 16436
  ), class = &quot;Date&quot;), HAM = c(1016.89391375364, -5069.52785909119, 
  3168.39687262048, -1265.24208195278, 1463.67252927616), State = c(&quot;Expansion&quot;, 
  &quot;Contraction&quot;, &quot;Expansion&quot;, &quot;Contraction&quot;, &quot;Expansion&quot;), sd = c(&quot;larger&quot;, 
  &quot;smaller&quot;, &quot;larger&quot;, &quot;smaller&quot;, &quot;larger&quot;)), row.names = c(1L, 
  4L, 5L, 6L, 8L), class = &quot;data.frame&quot;)
  </code></pre>