diff --git a/R/Threshold.R b/R/Threshold.R index aa43b3a0f989c05c11b1c0db84bd4a986eed0b71..7b94021318200a270dcda32f1c79f6fd96837b3c 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 = FALSE) { + tapply(X = data, INDEX = indices, FUN = quantile, probs = qtiles, na.rm = na.rm) }