Alexander - 1 year ago 75
R Question

Finding Row number of Consecutive decreasing values in data

I need to detect from data the first element of the first sequence of length 5 of consecutively decreasing numbers. There is a similar post here but when I applied to my data it failed.

``````set.seed(201)
az <- c(sort(runif(10,0,0.9),decreasing = T),sort(runif(3,-0.3,0),decreasing = T),sort(runif(3,-0.3,0),decreasing = F),sort(runif(4,-0.3,0),decreasing = T),sort(runif(4,-0.3,0),decreasing = F),sort(runif(6,-0.3,0),decreasing = T))
tz <- seq(1,length(az))
df <- data.frame(tz,az=round(az,2))
``````

In the figure above it would be somewhere around tz = 25.

The post says that this function need to improve and so far I cannot get my desired result!

``````  getFirstBefore<-function(x,len){
r<-rle(sign(diff(x)))
n<-which(r\$lengths>=len & r\$values<0)
if(length(n)==0)
return(-1)
1+sum(r\$lengths[seq_len(n[1]-1)])
}

df1 <- df%>%
mutate(cns_tz=getFirstBefore(az,5))

tz    az cns_tz
#1   1  0.56      4
#2   2  0.55      4
#3   3  0.33      4
#4   4  0.33      4
#5   5  0.26      4
#6   6  0.15      4
#7   7  0.12      4
#8   8  0.09      4
#9   9  0.04      4
#10 10  0.04      4
#11 11 -0.10      4
#12 12 -0.12      4
#13 13 -0.16      4
#14 14 -0.16      4
#15 15 -0.14      4
#16 16 -0.14      4
#17 17 -0.13      4
#18 18 -0.15      4
#19 19 -0.22      4
#20 20 -0.30      4
#21 21 -0.12      4
#22 22 -0.12      4
#23 23 -0.11      4
#24 24 -0.07      4
#25 25 -0.05      4
#26 26 -0.09      4
#27 27 -0.10      4
#28 28 -0.15      4
#29 29 -0.17      4
#30 30 -0.22      4
``````

We can use `rleid` from `data.table`

``````library(data.table)
n <- 5
v1 <- setDT(df)[sign(az)<0,  .I[which(.N==n)] , rleid(c(1, sign(diff(az))))]\$V1[1L]
v1
#[1] 26
df[, cnz_tz := v1]
``````

Or another option is `shift` with `Reduce`

``````setDT(df)[, cnz_tz := .I[Reduce(`&`, shift((az - shift(az, fill=az[1])) < 0,
0:4, type = "lead", fill=FALSE)) & sign(az) < 0][1]]
``````

We can also use `rleid` in `dplyr`

``````library(dplyr)
v1 <- df %>%
group_by(rl= rleid(c(1, sign(diff(az))))) %>%
mutate(rn =  sign(az) < 0 & n()==5) %>%
.\$rn %>%
which() %>%