I have a data frame with 2718 observations of which one column is of interest. It is a first difference series created with `diff()`. For ease, let's create a fake vector that resembles the data and pretend `v` is a first difference series. The `NA`s are introduced to make it similar to the original data. 

```
# Create fake first difference series vector v
v <- runif(2718, -0.05, 0.05)
v <- append(NA, diff(v))

# Insert NAs at the beginning and end
v[c(1:8, 2712:2718)] <- NA

# Insert some NAs at random places in v
ind <- which(v %in% sample(v, 7))
v[ind] <- NA

```

I am interested in the sequences of `v` that show an increasing and decreasing behaviour. Specifically, I would like to extract the sequences of `v` that consecutively increase and decrease, respectively. In an increasing sequence, each element of `v` cannot be less than its preceding element and in a decreasing sequence, each element of `v` cannot be greater than its preceding element. Try to picture this when plotting `v`: 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.

To clarify, the procedure may be explained by this:

* Starting from the beginning, look at a given value `i` in `v` and compare it to the preceding one `i-1`
* If `i` is greater than or equal to `i-1`, the sequence qualifies as increasing; if `i` is less than or equal to `i-1`, the sequence qualifies as decreasing.
* Store the increase/decrease for such a section by extracting the `i`th element
* 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
* If there is no change from to `i-1` to `i` (i.e. `i-1` and `i` are equal), the sequence continues, just as it does when a `NA` occurs

As `v` is a first difference series, the extracted element `i` (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.

I imagine storing the `i`th elements of `v` in a new vector (e.g. `inc.v` and `dec.v`) 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 `inc.v` or `dec.v` with respect to their original positions in `v`, so I can track them back. Each sequence in `inc.v` and `dec.v` should be easy to distinguish when they are separated by `NA` elements.

I tried writing this with a for loop and conditional statements but did not get far:

```
inc.v <- NULL
dec.v <- NULL
for (i in 2:length(v)) {
  if(!v[i] < v[i-1] | is.na(v[i])) {
    inc.v[i] <- v[i]
  } else if (!v[i] > v[i-1] | is.na(v[i])) {
    dec.v[i] <- v[i]
  } else {
    next
  }
}

```

The `if` and `else if` statements represent the fifth bullet point. I am aware of the problem that when `i` equals `i-1`, 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:

```
# A symmetric 50 points moving average for v
f50 <- rep(1/51,51)
v_smooth <- filter(v, f50, sides = 2)

```

When running the loop as of now, the evaluation of the first condition results in an `NA`, giving me the error: 

```
Error in if (!v[i] < v[i - 1] | is.na(v[i])) { : 
  missing value where TRUE/FALSE needed

```

I do not quite understand what's happening here because the `is.na()` statement should secure a `TRUE` or `FALSE` argument?!

Happy to hear your thoughts!