From 34772a6a95a3009225153494e77b61b8054d151f Mon Sep 17 00:00:00 2001 From: nperez Date: Thu, 11 Jul 2019 17:39:10 +0200 Subject: [PATCH 1/2] Threshold function works with NA values --- R/Threshold.R | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/R/Threshold.R b/R/Threshold.R index aa43b3a..2ba75e6 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -29,7 +29,7 @@ #'a <- Threshold(data, dates = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL) #'str(a) #'@export -Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL) { +Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qtiles = 0.9, ncores = NULL, na.rm = FALSE) { if (is.null(data)) { stop("Parameter 'data' cannot be NULL.") } @@ -93,6 +93,14 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt if (stop_error) { stop("Parameter 'dates' must be of the same length as the 'time' dimension of the parameter 'data'.") } + if (!is.logical(na.rm)) { + stop("Parameter 'na.rm' must be logical.") + } + if (length(na.rm) > 1) { + na.rm <- na.rm[1] + warning("Parameter 'na.rm' has length > 1 and only the first ", + "element will be used.") + } dates <- as.PCICt(dates, cal = calendar) dates = as.character(dates) jdays <- as.numeric(strftime(dates, format = "%j")) @@ -170,7 +178,7 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt if (length(dim(data)) > 1) { result <- Apply(data = data, margins = margins, fun = .Threshold, indices = jdays, qtiles = qtiles, - ncores = ncores) + ncores = ncores, na.rm = na.rm) names(dim(result$output1)) <- c("jdays", dim_names[-time_dim]) } else { result <- list() @@ -179,6 +187,6 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt } return(result$output1) } -.Threshold <- function(data, indices, qtiles) { - tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles) +.Threshold <- function(data, indices, qtiles, na.rm) { + tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles, na.rm = na.rm) } -- GitLab From a2001677c4293c36acedbe0c53f3c6114dcbd00a Mon Sep 17 00:00:00 2001 From: nperez Date: Fri, 12 Jul 2019 10:11:31 +0200 Subject: [PATCH 2/2] na.rm set default --- R/Threshold.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/Threshold.R b/R/Threshold.R index 2ba75e6..7b94021 100644 --- a/R/Threshold.R +++ b/R/Threshold.R @@ -187,6 +187,6 @@ Threshold <- function(data, dates = NULL, calendar = NULL, base.range = NULL, qt } return(result$output1) } -.Threshold <- function(data, indices, qtiles, na.rm) { +.Threshold <- function(data, indices, qtiles, na.rm = FALSE) { tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles, na.rm = na.rm) } -- GitLab