An error occurred while loading the file. Please try again.
-
Dorchies David authored
- add pmedian (from pmin and pmax) Refs #12
#' Mean annual flow
#'
#' @description
#' The mean annual flow is established from the mean of all available daily flows.
#' A year is considered missing if more than 20% of the data is missing.
#'
#' @param x a [data.frame] with a first column containing dates in [POSIXt] format and one [numeric] column per gauging station
#' @param threshold [numeric] minimum ratio of available data in a year to take it into account
#' @param ... Use for S3 method compatibility
#'
#' @return A [numeric] named [vector] with the mean annual flow for each gauging station
#' @export
#' @rdname calcQA
#'
#' @source Chazot, Sébastien, Perrin, Charles, Jean-Philippe Vidal, Eric Sauquet, Mathilde Chauveau, et Nathalie Rouchy. 2012. « Explore 2070 - Lot Hydrologie de surface - A2 - Résultats : Fiches, Cartes et Graphes ». Ministère de l’écologie, du développement durable, des transports et du logement.
#'
calcQA <- function(x, threshold = 0.8, ...) {
UseMethod("calcQA", x)
}
#' @export
#' @rdname calcQA
calcQA.data.frame <- function(x, threshold = 0.8, ...) {
QA <- SeriesAggreg(x, Format = "%Y", rep("mean_na_rm", ncol(x)-1))
nDaysNotNA <- SeriesAggreg(x, Format = "%Y", rep("not_na_count", ncol(x)-1))
QA <- QA[, -1]
nDaysNotNA <- nDaysNotNA[, -1]
QA[nDaysNotNA < threshold * 365] <- NA
apply(QA, 2, mean_na_rm)
}
#' Count non-NA values in an object
#'
#' @param x the object where to count available values
#'
#' @return The number of non-NA values
#' @export
#'
#' @examples
#' not_na_count(c(1, 2, 3, NA, 4))
#'
not_na_count <- function(x) {
sum(!is.na(x))
}
#' Arithmetic Mean with NA stripped before computation
#'
#' @param x An R object.
#'
#' @return See [mean]
#' @export
#'
#' @examples
#' mean_na_rm(c(1, 2, 3, NA, 4))
#'
mean_na_rm <- function(x) {
mean(x, na.rm = TRUE)
}