You can create a new column that is called, which is above the median, and then accept only those that are sequential and higher
> foo <- as_tibble(data.table(x = c(1,1,5,1,5,5,1), seq(as.Date("2016-01-01"), length = 7, by = "days")))
Step 1
Create a column to find those above the median
> foo$higher_than_median <- foo$x > median(foo$x)
Step 2
Compare this column with diff ,
Take it only when both are successively higher or lower .. c(0, diff(foo$higher_than_median) == 0
Then add the condition that they must be higher foo$higher_than_median == TRUE
Full expression:
foo$both_higher <- c(0, diff(foo$higher_than_median)) == 0 & $higher_than_median == TRUE
Step 3
To find the probability, take the average foo$both_higher
mean(foo$both_higher) [1] 0.1428571
source share