-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/IDEMSInternational/rpicsa
- Loading branch information
Showing
17 changed files
with
312 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Get Extreme Data | ||
#' | ||
#' This function identifies extreme values in a specified element (column) of a data frame. It can operate in two modes: percentile-based and threshold-based. | ||
#' | ||
#' @param data A data frame containing the data to be analysed. | ||
#' @param element The name of the column in 'data' for which extremes are to be found. | ||
#' @param type A character string specifying the mode of operation. It can be either `"percentile"` or `"threshold"`. Here, `"percentile"` identifies values above a certain percentile (e.g., 95th percentile); `"threshold"` identifies values above a specific threshold value. | ||
#' @param value A numeric value specifying the percentile or threshold, depending on the 'type' parameter. If `type == "percentile"`, `value` is the percentile (e.g., 95 for 95th percentile). If `type == "threshold"`, `value` is the threshold value (e.g., 50 mm for rainfall). | ||
#' @param direction A character string specifying the direction for the operation. It can be either `"greater"` or `"less"`. | ||
#' | ||
#' @export | ||
#' @return A filtered data frame where the `element` values are considered extreme based on the specified `type` and `value`. | ||
#' | ||
#' @examples | ||
#' # data(daily_niger) | ||
#' # filtered_data <- get_extremes(data = daily_niger, element = "rain", type = "threshold", value = 50) | ||
get_extremes <- function(data, element, type = c("percentile", "threshold"), value = 95, direction = c("greater", "less")) { | ||
type <- match.arg(type) | ||
direction <- match.arg(direction) | ||
|
||
# Check if element exists in data | ||
if (!element %in% names(data)) { | ||
stop("Element column not found in the data frame.") | ||
} | ||
|
||
# Determine the threshold value based on the specified type | ||
if (type == "percentile") { | ||
threshold_value <- quantile(data[[element]], probs = value/100, na.rm = TRUE) | ||
} else { | ||
threshold_value <- value | ||
} | ||
|
||
# Filter data based on the threshold and direction | ||
if (direction == "greater") { | ||
extreme_data <- data %>% dplyr::filter(.data[[element]] > threshold_value) | ||
} else { | ||
extreme_data <- data %>% dplyr::filter(.data[[element]] < threshold_value) | ||
} | ||
|
||
return(extreme_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Summary Temperature (Month or annually) | ||
#' @description Returns a summary data frame giving either the mean of the minimum and/or maximum temperatures each year from 1 Jan to 31 Dec, or by year and month. | ||
#' | ||
#' @param data The data.frame to calculate from. | ||
#' @param date_time \code{\link[base]{Date}} The name of the date column in \code{data}. | ||
#' @param tmin \code{character(1)} The name of the minimum temperature column in \code{data} to apply the function to. | ||
#' @param tmax \code{character(1)} The name of the maximum temperature column in \code{data} to apply the function to. | ||
#' @param year \code{character(1)} The name of the year column in \code{data}. If \code{NULL} it will be created using \code{lubridate::year(data[[date_time]])}. | ||
#' @param month \code{character(1)} The name of the month column in \code{data}. If \code{NULL} it will be created using \code{lubridate::month(data[[date_time]])}. | ||
#' @param station \code{character(1)} The name of the station column in \code{data}, if the data are for multiple station. | ||
#' @param to \code{character(1)} Default `annual`. The period of time to calculate the mean temperature columns over (options are `annual` or `monthly`). | ||
#' @param summaries \code{character} The summaries to display. Options are `"mean"`, `"max"`, `"min"`. | ||
#' @param na_rm \code{logical(1)}. Should missing values (including \code{NaN}) be removed? | ||
#' @param na_prop \code{integer(1)} Max proportion of missing values allowed | ||
#' @param na_n \code{integer(1)} Max number of missing values allowed | ||
#' @param na_consec \code{integer(1)} Max number of consecutive missing values allowed | ||
#' @param na_n_non \code{integer(1)} Min number of non-missing values required | ||
#' | ||
#' @return A data.frame with mean summaries for each year or year and month for the minimum daily temperature and/or the maximum daily temperature. | ||
#' @export | ||
#' | ||
#' @examples #daily_niger_1 <- daily_niger %>% filter(year < 1950) | ||
#' #mean_temperature(data = daily_niger_1, date_time = "date", station = "station_name", | ||
#' # tmax = "tmax", tmin = "tmin", na_prop = 0.05) | ||
|
||
mean_temperature <- function(data, date_time, tmin = NULL, tmax = NULL, year = NULL, | ||
month = NULL, station = NULL, to = c("annual", "monthly"), | ||
summaries = c("mean", "min", "max"), na_rm = FALSE, | ||
na_prop = NULL, na_n = NULL, na_consec = NULL, na_n_non = NULL) { | ||
to <- match.arg(to) | ||
summaries_all <- c() | ||
if ("mean" %in% summaries){ summaries_all <- cbind(summaries_all, mean = "mean")} | ||
if ("min" %in% summaries){ summaries_all <- cbind(summaries_all, min = "min")} | ||
if ("max" %in% summaries){ summaries_all <- cbind(summaries_all, max = "max")} | ||
climatic_summary(data = data, date_time = date_time, | ||
station = station, elements = c(tmin, tmax), | ||
year = year, month = month, to = to, | ||
summaries = c(mean = "mean"), na_rm = na_rm, | ||
na_prop = na_prop, na_n = na_n, | ||
na_n_non = na_n_non, names = "{.fn}_{.col}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.