X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2Futils.R;h=64c3c0a1b5c586e009cc8ae44ff271a0c1a7f2b1;hp=80a7e5df1e2774fbd54217044ef862ccc0d09c07;hb=98e958cab563866f8e00886b54336018a2e8bc97;hpb=af3b84f4cacade7d83221ca0249b546c50ddf340 diff --git a/pkg/R/utils.R b/pkg/R/utils.R index 80a7e5d..64c3c0a 100644 --- a/pkg/R/utils.R +++ b/pkg/R/utils.R @@ -8,10 +8,12 @@ #' @export dateIndexToInteger = function(index, data) { + #works on integers too: trust input if (is.numeric(index)) index = as.integer(index) if (is.integer(index)) - return (index) #works on integers too: trust input + return (index) + if (inherits(index, "Date") || is.character(index)) { tryCatch(indexAsDate <- as.Date(index), error=function(e) stop("Unrecognized index format")) @@ -35,8 +37,12 @@ dateIndexToInteger = function(index, data) #' @export integerIndexToDate = function(index, data) { + #works on dates too: trust input + if (is.character(index)) + index = as.Date(index) if (is(index,"Date")) - return (index) #works on dates too: trust input + return (index) + index = index[1] if (is.numeric(index)) index = as.integer(index) @@ -102,3 +108,19 @@ getSimilarDaysIndices = function(index, limit, same_season) #' @export getSerie = function(data, index) data[[index]]$centered_serie + data[[index]]$level + +#' getNoNA2 +#' +#' Get indices in data of no-NA series followed by no-NA, within [first,last] range. +#' +#' @param data Object of class Data +#' @param first First index (included) +#' @param last Last index (included) +#' +#' @export +getNoNA2 = function(data, first, last) +{ + (first:last)[ sapply(first:last, function(i) + !any( is.na(data$getCenteredSerie(i)) | is.na(data$getCenteredSerie(i+1)) ) + ) ] +}