Skip to content

Commit

Permalink
Fix Laurent
Browse files Browse the repository at this point in the history
  • Loading branch information
philouail committed Mar 19, 2024
1 parent ee9c813 commit 62339b5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
31 changes: 24 additions & 7 deletions R/force_sorted.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@
#'
#' @description
#' This function performs interpolation on the non-increasing parts of an
#' input vector to ensure its values are continuously increasing.
#' input vector to ensure its values are monotonously increasing.
#' If the values are non-increasing at the end of the vector, these values
#' will be monotonously increasing by the value defined with parameter `by `
#' from the last increasing value.
#'
#' @param x `numeric` vector.
#'
#' @param by `numeric(1)` value that will determine the monotonous increase in
#' case the values at the end of the vector are non-increasing and
#' therefore interpolation would not be possible. Default
#' to `by = 0.000001`.
#'
#' @return A vector with continuously increasing values.
#'
#' @note
#' NA values will not be replaced.
#'
#' @examples
#' x <- c(NA, NA, NA, 1.2, 1.1, 1.14, 1.2, 1.3, NA, 1.04, 1.4, 1.6, NA, NA)
#' sorted_rtime <- force_sorted(x)
#' sorted_vec <- force_sorted(x)
#' is.unsorted(x, na.rm = TRUE)
#'
#' ## Vector non increasing at the end
#' x <- c(1, 2, 1.5, 2)
#' sorted_rtime <- force_sorted(x, by = 0.1)
#' is.unsorted(x, na.rm = TRUE)
#'
#' ## We can see the values were not interpolated but rather replaced by the
#' ## last increasing value `2` and increasing by 0.1.
#' sorted_vec
#'
#' @export
#'
#' @rdname force_sorted
force_sorted <- function(x){
force_sorted <- function(x, by = 0.000001) {
# Select only the non-NA values
if (!is.numeric(x) && !is.integer(x))
stop("'x' needs to be numeric or integer")
Expand All @@ -31,13 +48,13 @@ force_sorted <- function(x){
# Find next biggest value
next_idx <- which(vec_temp > vec_temp[idx])[1L]

if (is.na(next_idx)){
if (is.na(next_idx)) {
l <- idx:length(vec_temp)
vec_temp[l] <- seq(vec_temp[idx], by = 0.000001,
vec_temp[l] <- seq(vec_temp[idx], by = by,
length.out = length(l))
warning("Found decreasing values at the end of vector, ",
"interpolation not possible. Replacing values. See help ",
"for more details")
"interpolation not possible there. Increasing values by",
by, " instead. See help for more details")
break
}
# Interpolation
Expand Down
23 changes: 20 additions & 3 deletions man/force_sorted.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62339b5

Please sign in to comment.