I am trying to find the most effective way to solve the following. Suppose we have some data that looks like this:
d1 <- seq(0, 3000, length.out = 1000) d2 <- cos(seq(0, 6*pi, length.out = 1000))*rev(seq(0, 1, length.out = 1000)) dd <- as.data.frame(cbind(d1, d2))

I need to determine from d2 first element of the first sequence of length 20 consecutively increasing numbers. In the figure above, it will be somewhere around d1 = 500 . My current approach is based on this function:
getFirstBeforeSequence <- function(x, y, len){ a1 <- cbind(lapply(split(y, cumsum(c(1, diff(y) < 0))), length)) a2 <- which(a1 > len)[1]-1 a3 <- sum(unlist(a1)[1:a2])+1 a3 }
This function gives me the desired result, the element is at position 164 and occurred when d1 = 489.4895 :
getFirstBeforeSequence(dd$d1, dd$d2, 20) # 164 dd$d1[164] # 489.4895
However, I had the impression that my decision was too complicated, and I am sure that others will have better solutions. Any help would be greatly appreciated.
r
VLC
source share