Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Row number of Consecutive decreasing values in data

Tags:

r

diff

dplyr

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))

enter image description here

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
like image 843
Alexander Avatar asked Sep 06 '16 07:09

Alexander


1 Answers

I would sort every 5 consecutive values, and see if that matches with the unsorted data. Then find the first occurance of such a match:

set.seed(123)
test <- rnorm(100)

decr <- sapply(seq_along(test),function(x){all(sort(test[x:(x+5)],decreasing = T) == test[x:(x+5)])})
firstdecr <- min(which(decr)):(min(which(decr))+5)
plot(test)
lines(firstdecr, test[firstdecr], col="red")

Only flaw I can see if there are equal values in a 5 value epoch, but you could also test for that.

like image 77
Wietze314 Avatar answered Oct 12 '22 02:10

Wietze314