title: Extract increasing and decreasing sequences from vector
body: <p>I have a data frame with 2718 observations of which one column is of interest. It is a first difference series created with <code>diff()</code>. For ease, let's create a fake vector that resembles the data and pretend <code>v</code> is a first difference series. The <code>NA</code>s are introduced to make it similar to the original data. </p>
  
  <pre><code># Create fake first difference series vector v
  v &lt;- runif(2718, -0.05, 0.05)
  v &lt;- append(NA, diff(v))
  
  # Insert NAs at the beginning and end
  v[c(1:8, 2712:2718)] &lt;- NA
  
  # Insert some NAs at random places in v
  ind &lt;- which(v %in% sample(v, 7))
  v[ind] &lt;- NA
  </code></pre>
  
  <p>I am interested in the sequences of <code>v</code> that show an increasing and decreasing behaviour. Specifically, I would like to extract the sequences of <code>v</code> that consecutively increase and decrease, respectively. In an increasing sequence, each element of <code>v</code> cannot be less than its preceding element and in a decreasing sequence, each element of <code>v</code> cannot be greater than its preceding element. Try to picture this when plotting <code>v</code>: Whenever the curve does not decrease (i.e. goes up or stays level), it is an increasing sequence and whenever the curve does not increase (i.e. goes down or stays level), it is a decreasing sequence.</p>
  
  <p>To clarify, the procedure may be explained by this:</p>
  
  <ul>
  <li>Starting from the beginning, look at a given value <code>i</code> in <code>v</code> and compare it to the preceding one <code>i-1</code></li>
  <li>If <code>i</code> is greater than or equal to <code>i-1</code>, the sequence qualifies as increasing; if <code>i</code> is less than or equal to <code>i-1</code>, the sequence qualifies as decreasing.</li>
  <li>Store the increase/decrease for such a section by extracting the <code>i</code>th element</li>
  <li>Each sequence is terminated once the next value is greater than (for a decreasing sequence) or less than (for an increasing sequence) the preceding one</li>
  <li>If there is no change from to <code>i-1</code> to <code>i</code> (i.e. <code>i-1</code> and <code>i</code> are equal), the sequence continues, just as it does when a <code>NA</code> occurs</li>
  </ul>
  
  <p>As <code>v</code> is a first difference series, the extracted element <code>i</code> (3rd bullet point) already represents the increase/decrease. For now, I do not want to limit the length of the sequences, hence a sequence might already be given by two elements.</p>
  
  <p>I imagine storing the <code>i</code>th elements of <code>v</code> in a new vector (e.g. <code>inc.v</code> and <code>dec.v</code>) and afterwards finding maximum and mean increase/decrease of the sequences, as well as maximum and mean length of these sequences. The elements should be stored in <code>inc.v</code> or <code>dec.v</code> with respect to their original positions in <code>v</code>, so I can track them back. Each sequence in <code>inc.v</code> and <code>dec.v</code> should be easy to distinguish when they are separated by <code>NA</code> elements.</p>
  
  <p>I tried writing this with a for loop and conditional statements but did not get far:</p>
  
  <pre><code>inc.v &lt;- NULL
  dec.v &lt;- NULL
  for (i in 2:length(v)) {
    if(!v[i] &lt; v[i-1] | is.na(v[i])) {
      inc.v[i] &lt;- v[i]
    } else if (!v[i] &gt; v[i-1] | is.na(v[i])) {
      dec.v[i] &lt;- v[i]
    } else {
      next
    }
  }
  </code></pre>
  
  <p>The <code>if</code> and <code>else if</code> statements represent the fifth bullet point. I am aware of the problem that when <code>i</code> equals <code>i-1</code>, it can qualify as both an increasing and decreasing sequence and it should be added to whatever sequence was stored previously. I just have no idea how to implement that. I think the sequences will be quite short as the data is noisy and periods of no decrease/no increase won't prevail for long. Hence, it might be a good idea to also try this operation with e.g. a 50/100 points moving mean:</p>
  
  <pre><code># A symmetric 50 points moving average for v
  f50 &lt;- rep(1/51,51)
  v_smooth &lt;- filter(v, f50, sides = 2)
  </code></pre>
  
  <p>When running the loop as of now, the evaluation of the first condition results in an <code>NA</code>, giving me the error: </p>
  
  <pre><code>Error in if (!v[i] &lt; v[i - 1] | is.na(v[i])) { : 
    missing value where TRUE/FALSE needed
  </code></pre>
  
  <p>I do not quite understand what's happening here because the <code>is.na()</code> statement should secure a <code>TRUE</code> or <code>FALSE</code> argument?!</p>
  
  <p>Happy to hear your thoughts!</p>