Answer to: Creating a smoother profile of a set of number using R
Score: 2
Take the cumulative minimum of the first p points and then the cumulative maximum of the vector formed from the last cumulative minimum followed by the remaining points and then at the end omit the the first of these cumulative maximum points. Concatenate those two vectors to get the result.
f <- function(x, p) {
mn <- cummin(head(x, p))
mx <- cummax(c(mn[p], tail(x, -p)))[-1]
c(mn, mx)
}
x <- c(3, 4, 2, 10, 1, 23, 11, 44)
f(x, 4)
## [1] 3 3 2 2 2 23 23 44
f(x, length(x))
## [1] 3 3 2 2 1 1 1 1
f(x, 1)
## [1] 3 4 4 10 10 23 23 44
View Question ↗
Question
Parent Entity
Score: 3 • Views: 106
Site: stackoverflow
SaaS Metrics