Find the minma /valley points and get the index where the valley starts and valley ends in R

浪子不回头ぞ 提交于 2021-02-16 15:29:06

问题


I am kind of new to Statistics and R.I have a requirement to find the peaks and valleys and the index where the peak/valley starts and ends.

For the Maxima/peak i got the findPeaks function which helps me with the peak requirement.But i am unable to find any packages for finding the valley points that suits my requirement.

The following is the R function for finding the peaks.

   function (x, nups = 1, ndowns = nups, zero = "0", peakpat = NULL, 
    minpeakheight = -Inf, minpeakdistance = 1, threshold = 0, 
    npeaks = 0, sortstr = FALSE) 
   {
    stopifnot(is.vector(x, mode = "numeric"))
    if (!zero %in% c("0", "+", "-")) 
        stop("Argument 'zero' can only be '0', '+', or '-'.")
    xc <- paste(as.character(sign(diff(x))), collapse = "")
    xc <- gsub("1", "+", gsub("-1", "-", xc))
    if (zero != "0") 
        xc <- gsub("0", zero, xc)
    if (is.null(peakpat)) {
        peakpat <- sprintf("[+]{%d,}[-]{%d,}", nups, ndowns)
    }
    rc <- gregexpr(peakpat, xc)[[1]]
    if (rc[1] < 0) 
        return(NULL)
    x1 <- rc
    x2 <- rc + attr(rc, "match.length")
    attributes(x1) <- NULL
    attributes(x2) <- NULL
    n <- length(x1)
    xv <- xp <- numeric(n)
    for (i in 1:n) {
        xp[i] <- which.max(x[x1[i]:x2[i]]) + x1[i] - 1
        xv[i] <- x[xp[i]]
    }
    inds <- which(xv >= minpeakheight & xv - pmax(x[x1], x[x2]) >= 
        threshold)
    X <- cbind(xv[inds], xp[inds], x1[inds], x2[inds])
    if (minpeakdistance != 1) 
        warning("Handling 'minpeakdistance' has not yet been implemented.")
    if (sortstr) {
        sl <- sort.list(X[, 1], na.last = NA, decreasing = TRUE)
        X <- X[sl, , drop = FALSE]
    }
    if (npeaks > 0 && npeaks < nrow(X)) {
        X <- X[1:npeaks, , drop = FALSE]
    }
    if (length(X) == 0) 
        return(c())
    else return(X)
}

I believe a slight tweak is required to get this code converted to finding the minima/valley points in the dataset.I tried and tried but failed! Sending our all statisticians an SOS

Thanks

EDIT:

Attaching the Data :

dat <- c(85.58, 89.75, 79.09, 25.52, 26.62, 28.8, 29.88, 27.72, 24.75, 
28.58, 27.88, 27.14, 29.68, 25.97, 24.67, 29.62, 29.7, 25.44, 
25, 25.85, 29.64, 29.32, 26.85, 29.55, 28.85, 27.35, 29.02, 25.65, 
27.19, 26.42, 24.8, 29.3, 27.05, 28.53, 28.76, 24.32, 29.28, 
26.6, 24.43, 29.29, 25.08, 77.92, 75.07, 81.1, 81.45, 79.97, 
83.36, 74.41, 82.06, 28.22, 28.43, 25.22, 24.21, 27.36, 27.37, 
24.43, 28.12, 26.37, 26.67, 25.3, 25.76, 27.36, 26.3, 29.06, 
25.58, 28.2, 27.53, 27.67, 26.1, 24.24, 28.27, 29.31, 29.22, 
25.87, 24.59, 29.38, 24.2, 26.03, 28.35, 25.09, 28.31, 29.2, 
27.23, 30, 29.5, 25.42, 27.25, 29.6, 81.24, 75.97, 87.94, 84.4, 
75.88, 72.99, 82.12, 72.3, 29.89, 25.59, 26.67, 27.08, 25.85, 
29.6, 24.6, 25.64, 28.87, 27.74, 24.05, 24.92, 24.27, 26.65, 
26.77, 28.38, 25.44, 29.13, 24.81, 25.38, 24.37, 24.67, 26.01, 
25.67, 24.49, 25.17, 28.77, 27.8, 26.22, 27.8, 24.35, 25.33, 
28.32, 24.71, 27.31, 29.51, 29.18, 29, 74.29, 78.29, 81.06, 84.9, 
76.93, 80.91, 78.69, 87.06, 26.49, 28.27, 27.58, 29.15, 29.98, 
27.87, 28.22, 28.23, 26.33, 24.29, 28.86, 25.35, 28.71, 28.22, 
24.14, 27.54, 27.69, 26.33, 27.22, 25.26, 26.25, 25.39, 27.94, 
29.36, 28.53, 25.15, 29.9, 27.46, 25.2, 27.22, 26.24, 28.13, 
24.28, 24.48, 26.75, 26.19, 24.22, 28.62, 28.03, 72.99, 80.09, 
72.19, 76.42, 79.31, 86.07, 80.18, 78.94, 26.39, 25.04, 26.93, 
26.45, 29.94, 28.05, 25.55, 26.5, 29.07, 26.72, 26.02, 24.61, 
29.88, 29.95, 25.74, 29.56, 25.94, 24.82, 27.82, 26.98, 27.57, 
28.85, 27, 26.81, 26.16, 25.28, 25.02, 26.51, 24.24, 28.51, 24.37, 
29.26, 24.48, 24.46, 25.94, 28.77, 28.75, 29.85, 75.98, 87.89, 
74.18, 89.5, 76.71, 89.82, 81.33, 79.36, 25.9, 27.42, 26.44, 
26.55, 25.61, 26.76, 25.31, 24.25, 29.92, 25.47, 28.23, 29.35, 
29.58, 26.79, 28.08, 28.94, 27.04, 26.85, 28.2, 29.65, 27.08, 
24.1, 29.59, 29.44, 24.36, 29.44, 26.19, 28.11, 26.47, 26.36, 
29.47, 25.61, 29.6, 26.5, 24.32, 24.29, 26.35, 27.91, 27.26, 
89.4, 88.21, 83.4, 87.77, 82.99, 79.53, 84.86, 29.19, 28.09, 
26.84, 25.43, 26.26, 29.43, 26.79, 24.66, 24.51, 27.58, 26.34, 
27.12, 26.14, 26.22, 25.02, 29.77, 25.01, 24.8, 28.3, 28.96, 
28.03, 29.93, 28.87, 26.44, 29.85, 27.92, 25.22, 24.78, 25.4, 
29.24, 24.29, 27.11, 28.46, 28.79, 24.82, 29.54, 27.5, 27.69, 
27.86, 86.92, 88.42, 74.91, 72.93, 88.29, 80.74, 76.05, 89.93, 
29.16, 25.64, 26.58, 27.75, 28.81, 29.66, 27.12, 25.46, 25.94, 
24.38, 29.58, 24.72, 29.25, 26.62, 29.42, 29.57, 26.76, 28.51, 
28.16, 29.24, 28.63, 29.42, 28.48, 29.81, 26.08, 24.22, 28.76, 
29.31, 27.82, 29.44, 25.71, 26.16, 29.55, 27.21, 25.8, 25.62, 
29.27, 27.59, 28.15, 88.87, 75.6, 89.42, 85.22, 85.75, 74.52, 
72, 26.85, 29.85, 27, 28.66, 29.58, 24.79, 25.67, 25.79, 25.84, 
28.43, 28.18, 26.18, 26.14, 26.37, 29.78, 25.23, 28.68, 25.25, 
28.62, 28.83, 25.17, 25.82, 25.35, 27.11, 27.61, 27.4, 26.76, 
29.66, 27.41, 28.18, 29.41, 26.12, 27.87, 24.76, 24.37, 28.32, 
25.44, 26.98, 26.1, 78.66, 79.26, 77.16, 77.45, 75.18, 89.97, 
75.39, 74.8, 27.89, 27.7, 26.8, 29.65, 24.28, 24.17, 29.54, 25.71, 
25.85, 27.33, 25.73, 26.99, 26.91, 29.02, 26.46, 26.49, 26.92, 
27.91, 25.06, 24.51, 25.84, 26.35, 27.17, 29.94, 24.93, 24.87, 
28.13, 27.85, 25.02, 28.22, 26.2, 24.75, 26.01, 24.68, 24.81, 
25.54, 27.59, 29.92, 27.94, 87.86, 89.46, 81.21, 74.62, 81.92, 
75.75, 77.49, 25.28, 27.09, 25.77, 28.16, 26.02, 28.4, 29.36, 
26, 29.84, 24.12, 24.66, 28.59, 26.61, 28.12, 25.44, 25.8, 28.06, 
28.09, 26.92, 27.85, 28.56, 27.87, 27.54, 28.96, 29.54, 27.15, 
28.52, 24.79, 24.01, 26.38, 26.85, 25.38, 24.52, 29.25, 29.85, 
29.86, 27.19, 27.27, 25.48, 87.65, 79.63, 81.5, 80.83, 72.88, 
86.28, 84.52, 78.93, 28.98, 29.31, 24.46, 29.05, 28.24, 25.93, 
27, 27.65, 28.42, 29.54, 27.56, 26.74, 26.84, 25.48, 27.07, 28.27, 
26.74, 26.51, 25.18, 26.38, 24.33, 26.67, 24.39, 28.01, 25.01, 
24.01, 25.08, 27.44, 29.91, 29.51, 29.04, 25, 29.37, 25.94, 24.49, 
28.35, 24.42, 26.16, 78.72, 73.9, 89.09, 75.15, 86.72, 89.05, 
81.38, 78.3, 26.89, 24.4, 28.58, 28.41, 25.7, 25.25, 24.35, 26.29, 
28.92, 24.99, 25.53, 26.58, 27.38, 29.4, 24.59, 28.24, 28.58, 
26.93, 25.83, 26.21, 27.28, 28.36, 24.88, 24.79, 25.09, 24.37, 
25.98, 28.8, 28.09, 25.91, 28.44, 25.59, 29.3, 24.55, 29.31, 
29.28, 25, 27.89, 27.95, 87.47, 77.87, 86.38, 84.04, 81.13, 80.12, 
83.71, 76.34, 29.81, 29.8, 24.7, 28.97, 28.48, 29.87, 29.34, 
27.89, 25.04, 29.11, 25.31, 25.62, 25.92, 29.23, 26.71, 26.94, 
26.4, 29.5, 27.95, 25.56, 25.27, 25, 25.19, 29.88, 28.35, 24.54, 
25.58, 24.99, 29.78, 28.83, 26.77, 28.95, 27.71, 27.51, 29.91, 
26.49, 26.54, 24.03, 78.2, 86.23, 89.04, 78.11, 88.77, 75.89, 
89.33, 83.5, 28.62, 25.96, 24.62, 25.16, 28.66, 28.32, 27.96, 
24.6, 29, 27.15, 29.96, 27.73, 24.49, 24.5, 27.39, 24.59, 25.11, 
27.71, 28.55, 26.22, 26.53, 25.65, 28.43, 28.14, 27.99, 27.63, 
24.46, 27.97, 27.57, 25.48, 27.49, 25.31, 26.91, 24.18, 25.55, 
25.33, 25.56, 29.12, 28.25, 76.93, 74.66, 77.28, 78.59, 80.35, 
85.66, 87.4, 28.65, 29.22, 24.22, 29.3, 29.5, 26.62, 28.66, 26.09, 
28.95, 29.31, 26.9, 29.25, 28.62, 29.36, 26.54, 26.36, 29.26, 
28.78, 27.91, 26.55, 26.84, 29.2, 25.48, 28.22, 29.07, 25.63, 
28.19, 29.61, 25.32, 29.17, 25.02, 29.26, 28.37, 24.34, 25.52, 
24.28, 24.8, 27.83, 27.37, 74, 80.69, 76.04, 73.29, 86.05, 78.37, 
84.88, 87.61, 24.88, 24.76, 28.58, 29.25, 24.29, 27.21, 29.93, 
27.38, 28.76, 25.66, 28.25, 28.67, 28.71, 27.43, 29.88, 28.42, 
29.41, 25.02, 28.55, 26.16, 29.69, 26.37, 28.24, 29.41, 28.82, 
28.76, 29.54, 24.62, 29.76, 28.77, 25.85, 29.83, 29.19, 28.93, 
24.87, 29.03, 25.3, 29.95, 24.85, 86.21, 87.9, 74.43, 80.05, 
78.35, 87.25, 87.02, 24.96, 27.35, 29.08, 25.59, 24.78, 26.16, 
27.37, 26.88, 29.92, 28.94, 24.39, 28.7, 27.72, 29.23, 27.66, 
29.15, 28.5, 27.47, 26.02, 28.2, 24.13, 24.75, 25.49, 26.81, 
26.06, 24.95, 27.48, 24.84, 25.19, 26.99, 28.29, 29.62, 27.45, 
24.63, 24.25, 25.34, 27.09, 27.87, 29.52, 80.83, 85.45, 74.74, 
73.18, 82.03, 82.15, 73.22, 88.01, 26.83, 29.7, 28.19, 26.36, 
25.99, 25.42, 27.02, 24.38, 24.81, 25.69, 25.12, 27.82, 24.66, 
29.77, 25.45, 28.37, 28.34, 26.48, 29.48, 24.77, 27.21, 29.84, 
28.03, 28.73, 27.05, 26.1, 25.12, 29.68, 29.99, 24.68, 25.17, 
26.88, 26.44, 26.93, 29.13, 27.31, 29.92, 25.85, 25.66, 81.04, 
80.33, 80.1, 83.05, 74.95, 77.81, 89.72, 29.22, 24.36, 28.19, 
25.47, 29.76, 27.14, 28.62, 25.09, 25.67, 28.91, 29.92, 29.15, 
27.03, 26.92, 26.11, 26.04, 29.14, 26.41, 29.15, 28.12, 27.56, 
28.14, 25.94, 28.24, 25.53, 25.85, 24.72, 24.53, 27.51, 25.4, 
24.34, 29.36, 29.24, 25.45, 29.24, 28.24, 26.73, 24.25, 28.19, 
86.05, 82.78, 86.09, 89.29, 79.11, 74.54, 89.06, 86.2, 25.31, 
29.38, 27.64, 28.61, 24.65, 24.65, 27.05, 25.22, 27.12, 29.76, 
27.21, 29.74, 26.67, 25.63, 29.45, 26.01, 24.85, 28.81, 26.72, 
24.43, 30, 29, 27.56, 29.6, 28.03, 27.85, 28.8, 28.97, 26.59, 
25.26, 29.42, 25.16, 26.31, 28.18, 29.91, 29.45, 26.52, 29.62, 
28.15, 78.41, 89.59, 73.66, 88.22, 85.35, 85.14, 84.78, 25.91, 
28.56, 26, 25.48, 26.36, 25.26, 29.38, 24.41, 25.17, 29.73, 24.37, 
26.48, 24.92, 24.5, 25.6, 27.3, 24.2, 24.9, 26.58, 28.38, 24.7, 
28.53, 28.9, 27.86, 24.13, 26.98, 26.04, 25.47, 27.2, 25.83, 
25.25, 27.91, 28.31, 27.54, 29.7, 26.85, 29.89, 29.01, 25.68, 
77.03, 72.22, 83.13, 85.61, 81.35, 74.55, 86.07, 79.83, 28.43, 
25.58, 24.63, 25.97, 24.02, 27.3, 25.68, 28.95, 28.02, 28.07, 
29.67, 25.88, 28.53, 27.2, 29.73, 28.6, 29.35, 28.64, 26.58, 
26.92, 25.43, 28.87, 28.48, 27.53, 26.48, 24.72, 26.23, 26.97, 
26.19, 29.82, 26.93, 24.9, 24.75, 29.2, 26.7, 29.94, 28.1, 26.32, 
76.14, 73.95, 75.83, 87.68, 81.3, 87.28, 82.45, 75.18, 25.32, 
24.44, 24.03, 29.99, 26.33, 28.89, 24.8, 29.97, 28.17, 25.28, 
25.65, 25.46, 25.48, 27.48, 28.21, 24.64, 24.89, 29.54, 28.01, 
24.16, 29.03, 27.81, 26.28, 26.93, 29.49, 29.11, 25.7, 26.48, 
25.98, 25.25, 25.59, 29.31, 24.99, 28.56, 29.87, 28.66, 27.28, 
26.12, 27.48, 80.79, 81.35, 79.13, 77.14, 79.08, 86.74, 85.77, 
86.42, 27.29, 26.96, 27.26, 24.78, 25.36, 25.76, 28.71, 28.83, 
26.89, 28.06, 29.89, 27.63, 29.05, 27.07, 24.94, 26.28, 28.88, 
29.21, 28.71, 24.65, 27.21, 24.27, 29.85, 24.92, 28.52, 26.14, 
27.49, 26.71, 27.06, 26.97, 25.79, 27.31, 28.76, 24.36, 25.86, 
27.23, 25.92, 28.11, 88.07, 80.07, 88.78, 83.67, 80.93, 85.49, 
74.59, 85.57, 29.17, 25.78, 27.58, 27.3, 29.97, 26.35, 26.76, 
26.64, 25.36, 24.19, 24.1, 24.29, 24.68, 28.86, 27.36, 24.9, 
25.18, 24.36, 24.96, 29.93, 26.91, 28.83, 29.51, 28.97, 24.43, 
27.27, 27.18, 25.52, 25.36, 24.24, 24.79, 28.02, 25.35, 25.92, 
25.51, 28.32, 24.9, 27.58, 25.01, 87.82, 80.38, 88.86, 77.54, 
88.09, 77.86, 87.2, 26.43, 28.04, 25.47, 28.97, 28.68, 24.04, 
25.7, 27.99, 24.35, 25.52, 28.93, 28.22, 26.49, 27.95, 29.58, 
28.08, 28.2, 29.39, 29.81, 28.2, 25.55, 27.44, 27.36, 25.61, 
29.08, 28.73, 29.09, 26.48, 27.36, 25.35, 29.71, 28.37, 27.58, 
28.69, 27.7, 28.82, 29.33, 27.43, 28.53, 72.36, 79.94, 79.47, 
75.29, 76.15, 72.58, 72.6, 79.31, 28.96, 29.41, 24.64, 28, 25.91, 
25.04, 29.03, 24.9, 26.78, 27.63, 27.37, 27.99, 25.67, 24.77, 
25.37, 25.67, 29.3, 28.85, 24.39, 29.58, 28.05, 29.49, 24.07, 
24.74, 25.33, 28.08, 29.69, 26.66, 27.92, 27.93, 29.94, 24.82, 
28.72, 26.18, 27.41, 28.48, 29, 25.32, 24.53, 76.73, 78.37, 80.54, 
82.33, 84.38, 72.91, 76.37, 27.62, 26.04, 27.29, 26.34, 24.64, 
28.63, 25.1, 27.44, 29.2, 26.75, 26.36, 28.5, 26.76, 24.75, 29.31, 
27.41, 26.44, 29.02, 24.58, 24.11, 26.32, 28.77, 29.17, 28.96, 
26.88, 24.28, 25.77, 29.95, 26.89, 25.72, 29.43, 27.07, 28.82, 
29.77, 28.24, 27.02, 26.74, 25.45, 26.43, 73.16, 78.42, 83.34, 
86.09, 87.35, 86.86, 77.92, 77.54, 29.09, 26.8, 24.49, 26.51, 
28.27, 25.86, 25.91, 24.9, 28.55, 25.73, 24.27, 26.14, 25.75, 
27.86, 27.22, 28.29, 28.06, 26.23, 28.39, 27.56, 26.22, 27.79, 
27.64, 26.79, 24.33, 27.12, 24.78, 26.69, 24.27, 25.35, 28.41, 
28.75, 25.1, 29.07, 24.51, 25.14, 26.91, 25.63, 24.88, 84.2, 
74.39, 79, 72.05, 82.25, 81.25, 88.79, 28.11, 29.73, 29.41, 24.92, 
27.47, 28.87, 26.79, 29.12, 25.82, 27.13, 28.24, 25.76, 24.08, 
24.36, 25.72, 29.03, 29.9, 28.24, 24.26, 27.26, 25.45, 26.23, 
29.42, 28.62, 28.27, 28.91, 27.02, 25.71, 28.05, 29.62, 27.79, 
27.91, 25.21, 26.47, 28.42, 27.73, 26.67, 29.61, 27.95, 89.82, 
77.44, 74.62, 85.53, 84.11, 80.29, 85.9, 74.15, 26.37, 27.88, 
26.48, 27.17, 29.83, 29.86, 25.79, 28.72, 28.79, 24.89, 27.42, 
29.96, 25.97, 29.39, 29.73, 29.16, 27.16, 24.07, 26.74, 24.74, 
25.93, 28.13, 27.94, 29.12, 26.9, 24.78, 29.37, 29.73, 29.53, 
27.14, 28.97, 28.43, 27.41, 26.08, 26.17, 28.33, 25.34, 25.99, 
88.19, 86.73, 77.01, 82.35, 81.08, 76.69, 78.22, 89.55, 26.77, 
28.46, 27.1, 24.06, 27.56, 24.38, 24.22, 29.04, 28.42, 29.84, 
25.36, 24.3, 28.73, 24.52, 27.47, 25.78, 27.37, 24.74, 24.02, 
26.61, 24.66, 24.89, 29.19, 24.2, 27.62, 27.01, 27.48, 24.43, 
26.94, 29.1, 25.36, 26.54, 24.66, 27.5, 28.23, 28.73, 28.33, 
25.69, 28.25, 83.9, 79.38, 76.34, 75.51, 80.89, 78.23, 79.88, 
75.63, 26.17, 27.17, 25.21, 24.47, 29.28, 27.82, 28.52, 26.91, 
29.2, 28.58, 25.76, 27.57, 29.01, 29.4, 27.91, 28.51, 25.15, 
29.5, 28.07, 24.08, 25.32, 24.27, 29.37, 27.29, 25.96, 24.85, 
24.17, 25.69, 25.2, 29.69, 27.1, 26.96, 24.55, 27.67, 26.27, 
24.37, 29.55, 26, 75.72, 76.52, 86.41, 83.37, 78.21, 89.84, 74.39, 
88.02, 29.46, 28.83, 24.9, 26.52, 25.93, 29.87, 24.1, 27.01, 
25.09, 24.12, 26.74, 27.28, 24.91, 26.44, 27.59, 29.88, 25.94, 
25.95, 28.47, 25.59, 28.54, 25.51, 24.53, 25.07, 25.15, 24.76, 
24.67, 29, 26.49, 28.57, 27.16, 28.42, 26.15, 29.03, 29.28, 29.1, 
28.99, 26.72, 29.95, 73.86, 79.19, 83.61, 74.48, 85.2, 75.48, 
72.09, 72.35, 27.76, 24.12, 24.26, 26.14, 25.11, 24.63, 24.74, 
28.48, 25.04, 28.14, 27.76, 29.52, 24.14, 29.81, 26.54, 26.92, 
24.87, 27.16, 28.49, 27.17, 29.9, 26.54, 27.39, 27.3, 27.29, 
25.51, 27.96, 25.09, 28.22, 26.79, 29.49, 24.91, 29.63, 29.29, 
29.35, 25.65, 27.52, 26.28, 75.07, 74.82, 74.78, 78.46, 79.25, 
88.8, 87.14, 84.2, 24.18, 26.56, 29.13, 29.99, 24.43, 25.78, 
28.39, 24.9, 26.2, 29.69, 29.02, 29.43, 29.68, 28.84, 28.21, 
27.65, 29.72, 26.62, 26.04, 24.28, 26.92, 29.76, 24.72, 24.1, 
27.98, 28.63, 28.46, 27.04, 25.35, 28.44, 24.78, 25.88, 25.64, 
29.73, 25.62, 28.43, 25.92, 28.9, 26.98, 72.33, 84.53, 79.14, 
75.51, 82.61, 88.05, 85.76, 29.64, 24.81, 26.03, 27.85, 29.05, 
25.6, 28.36, 25.2, 25.48, 24.23, 26.92, 28.36, 26.22, 27.66, 
29.68, 26.65, 25.42, 27.61, 26.15, 27.94, 27.96, 25.14, 24.4, 
26.66, 29.75, 28.06, 25.97, 28.06, 29.43, 25.84, 29.52, 29.04, 
24.12, 24.97, 25.27, 26.92, 28.19, 27.09, 27.31, 87.97, 72.27, 
85.72, 76.84, 82.6, 75.16, 84.84, 84.99, 27.84, 26.37, 24.36, 
28.42, 27.77, 26.19, 27.47, 25.28, 27.82, 28.14, 26.85, 28.93, 
24.11, 27.69, 25.99, 25.35, 28.79, 26.37, 24.4, 29.51, 24.27, 
24.78, 29.58, 28.32, 25.88, 28.59, 29.31, 24.93, 29.25, 26.63, 
24.5, 26.98, 26.31, 24.49, 24.2, 26.58, 25.2, 29.73, 29.47, 80.58, 
83.28, 79.6, 84.67, 79.29, 72.34, 81.99, 24.8, 28.76, 24.25, 
28.99, 25.93, 29.16, 27.78, 26.61, 24.17, 26.01, 29.26, 25.08, 
28.16, 25.73, 26.41, 24.95, 27.3, 26.5, 28.55, 24.46, 24.51, 
27.79, 24.25, 29.31, 26.99, 25.33, 25.61, 29.5, 25.43, 24.76, 
29.97, 28.59, 24.28, 30, 28.64, 25.73, 25.58, 25.81, 24.91, 81.69, 
78.24, 81.38, 85.33, 81.53, 74.37, 81.45, 78.86, 29.58, 25.89, 
27.35, 24.96, 24.23, 28.61, 24.6, 25.27, 28.19, 28.1, 26.06, 
25.67, 26.98, 29.92, 29.6, 24.86, 25.83, 24.45, 27.35, 25.98, 
27.4, 28.54, 28.92, 28.05, 24.41, 27.07, 29.63, 28.2, 25.78, 
24.35, 24.63, 26.18, 27.75, 24.93, 27.32, 27.44, 25.73, 26.51, 
27.91, 81.07, 88.7, 72.24, 85.81, 75.23, 76.67, 83.04, 24.45, 
24.73, 24.44, 24.28, 26.68, 25.25, 26.65, 28.3, 29.27, 25.95, 
29.65, 28.65, 26.88, 27.49, 24.36, 27.07, 28.85, 29.4, 28.31, 
28.93, 27.35, 27.93, 24.11, 28.81, 29.84, 28.07, 25.93, 26.98, 
26.41, 26.71, 26.76, 25.22, 29.69, 24.35, 28.61, 29.19, 26.89, 
24.81, 27.85, 82.14, 88.55, 77.12, 86.5, 83.7, 85.69, 88.08, 
89.32, 25.26, 29.45, 24.69, 29.92, 24.7, 27.86, 28.4, 24.22, 
27.22, 29.41, 29.9, 29.72, 27.65, 27.75, 24.33, 26.81, 26.3, 
25.72, 27.57, 27.29, 29.96, 29.65, 26.47, 28.53, 29.05, 27.35, 
25.68, 26.99, 25.48, 24.25, 25.25, 25.15, 28.68, 29.81, 25.67, 
28.98, 27.88, 27.91, 84.92, 75.59, 72.35, 81.94, 85.24, 80.73, 
89.54, 85.78, 28.76, 27.64, 28.33, 28.54, 28.46, 27.4, 26.39, 
25.78, 29.83, 25.06, 28.94, 27.6, 25.72, 24.39, 24.3, 29.01, 
27.88, 25.5, 26.4, 24.09, 29.4, 25.51, 26.33, 26.61, 24.07, 27.84, 
27.59, 25.53, 29.11, 24.38, 26.22, 24.02, 27.75, 27.41, 27.86, 
29.03, 25.82, 29.02, 28, 76.13, 86.44, 78.47, 85.61, 82.49, 84.47, 
72.51, 80.97)

回答1:


Still don't see in which way it fails.

# limit the length of data for readability
dat <- head(dat, 50)

library(pracma)
p <- findpeaks(dat)
v <- findpeaks(-dat)
plot(dat, t = "l")
points(p[,2], p[,1], pch = "+", col = "red")
points(v[,2], -v[,1], pch = "o", col = "blue")
legend("top", c("peak", "valley"), pch=c("+", "o"), col = c("red", "blue"))



来源:https://stackoverflow.com/questions/33120367/find-the-minma-valley-points-and-get-the-index-where-the-valley-starts-and-vall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!