Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

R Tidy : Dynamic Sequential Threshold

$
0
0

I'm trying to find a tidy way to dynamically adjust a threshold as I "move" through a tibble using library(tidyverse). For example, imagine a tibble containing sequential observations:

example <- 
  tibble(observed = c(2,1,1,2,2,4,10,4,2,2,3))
example
# A tibble: 11 x 1
   observed
      <dbl>
 1        2
 2        1
 3        1
 4        2
 5        2
 6        4
 7       10
 8        4
 9        2
10        2
11        3

I'm trying to calculate a threshold that starts with the initial value (2) and increments by a prespecified amount (in this case, 1) unless the current observation is greater than that threshold in which case the current observation becomes the reference threshold and further thresholds increment from it. Here is what the final tibble would look like:

answer <- 
  example %>%
  mutate(threshold = c(2,3,4,5,6,7,10,11,12,13,14))
answer
# A tibble: 11 x 2
   observed threshold
      <dbl>     <dbl>
 1        2         2
 2        1         3
 3        1         4
 4        2         5
 5        2         6
 6        4         7
 7       10        10
 8        4        11
 9        2        12
10        2        13
11        3        14

I'm looking for the best way to do this using dplyr/tidy. All help is appreciated!

EDIT:

The answers so far are very close, but miss in the case that the observed values drop and increase again. For example consider the same tibble as example above, but with a 4 instead of a 3 for the final observation:

example <- 
  tibble(observed = c(2,1,1,2,2,4,10,4,2,2,4))
example
# A tibble: 11 x 1
   observed
      <dbl>
 1        2
 2        1
 3        1
 4        2
 5        2
 6        4
 7       10
 8        4
 9        2
10        2
11        4

The diff & cumsum method then gives:

example %>%
  group_by(gr = cumsum(c(TRUE, diff(observed) > thresh))) %>%
  mutate(thresold = first(observed) + row_number() - 1) %>%
  ungroup %>%
  select(-gr)

A tibble: 11 x 2
   observed thresold
      <dbl>    <dbl>
 1        2        2
 2        1        3
 3        1        4
 4        2        5
 5        2        6
 6        4        4
 7       10       10
 8        4       11
 9        2       12
10        2       13
11        4        4

Where the final threshold value is incorrect.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>