Created
June 2, 2017 05:57
-
-
Save AdeelK93/7e6e3e2a14dcb64b0c633b55593ac948 to your computer and use it in GitHub Desktop.
Jagged to monotonic time series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
as.monotonic <- function(x) { | |
while(sum(diff(x)<0)) { | |
lastMonotonic <- which(diff(x)<0) | |
firstNonMonotonic <- lastMonotonic[1] + 1 | |
# which chunk of the time series needs to be adjusted up | |
lastNonMonotonic <- ifelse(length(lastMonotonic)>1, lastMonotonic[2], length(x)) | |
monotonicRange <- firstNonMonotonic:lastNonMonotonic | |
# add last monotonic value to nonmonotonic chunk | |
x[monotonicRange] <- x[monotonicRange] + x[lastMonotonic[1]] | |
} | |
x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment