Skip to content

Commit

Permalink
.drop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Mar 7, 2024
1 parent 3fa0201 commit 9801eaf
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
7 changes: 4 additions & 3 deletions R/end_rains.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param rain \code{character(1)} The name of the rainfall column in \code{data} to apply the function to.
#' @param doy \code{character(1)} The name of the day of year column in \code{data} to apply the function to. If \code{NULL} it will be created using the \code{date_time} variable.
#' @param s_start_doy \code{numerical(1)} Default `NULL` (if `NULL`, `s_start_doy = 1`. The day of year to state is the first day of year.
#' @param drop \code{logical(1)} default `TRUE`. Whether to drop years where there are `NA` data for the rainfall.
#' @param start_day \code{numerical(1)} The first day to calculate from in the year (1-366).
#' @param end_day \code{numerical(1)} The last day to calculate to in the year (1-366).
#' @param output \code{character(1)} Whether to give the start of rains by day of year (doy), date, or both. Default `"doy"`.
Expand All @@ -20,7 +21,7 @@
#' @examples #TODO#
#' # is same as R-Instat
end_rains <- function(data, date_time, station = NULL, year = NULL, rain = NULL,
doy = NULL, s_start_doy = NULL,
doy = NULL, s_start_doy = NULL, drop = TRUE,
start_day = 1, end_day = 366, output = c("doy", "date", "both"),
interval_length = 1, min_rainfall = 10){

Expand Down Expand Up @@ -66,15 +67,15 @@ end_rains <- function(data, date_time, station = NULL, year = NULL, rain = NULL,
data[[year]] <- factor(data[[year]])
if (!is.null(station)){
end_of_rains <- data %>%
dplyr::group_by(.data[[station]], .drop = FALSE)
dplyr::group_by(.data[[station]], .drop = drop)
} else {
end_of_rains <- data
}

end_of_rains <- end_of_rains %>%
dplyr::mutate(roll_sum_rain = RcppRoll::roll_sumr(x = .data[[rain]], n = interval_length, fill = NA, na.rm = FALSE)) %>%
dplyr::filter((roll_sum_rain > min_rainfall) | is.na(x=roll_sum_rain)) %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = FALSE) %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = drop) %>%
dplyr::filter(.data[[doy]] >= start_day & .data[[doy]] <= end_day, .preserve = TRUE)

if (output == "doy"){
Expand Down
7 changes: 4 additions & 3 deletions R/end_season.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param rain \code{character(1)} The name of the rainfall column in \code{data} to apply the function to.
#' @param doy \code{character(1)} The name of the day of year column in \code{data} to apply the function to. If \code{NULL} it will be created using the \code{date_time} variable.
#' @param s_start_doy \code{numerical(1)} Default `NULL` (if `NULL`, `s_start_doy = 1`. The day of year to state is the first day of year.
#' @param drop \code{logical(1)} default `TRUE`. Whether to drop years where there are `NA` data for the rainfall.
#' @param start_day \code{numerical(1)} The first day to calculate from in the year (1-366).
#' @param end_day \code{numerical(1)} The last day to calculate to in the year (1-366).
#' @param output \code{character(1)} Whether to give the start of rains by day of year (doy), date, or both. Default `"doy"`.
Expand All @@ -26,7 +27,7 @@
#' #capacity = 60, water_balance_max = 0.5,
#' #evaporation_value = 5)
end_season <- function(data, date_time, station = NULL, year = NULL, rain = NULL,
doy = NULL, s_start_doy = NULL,
doy = NULL, s_start_doy = NULL, drop = TRUE,
start_day = 1, end_day = 366, output = c("doy", "date", "both"),
capacity = 60, water_balance_max = 0.5, evaporation = c("value", "variable"),
evaporation_value = 5, evaporation_variable = NULL){
Expand Down Expand Up @@ -71,7 +72,7 @@ end_season <- function(data, date_time, station = NULL, year = NULL, rain = NULL

if (!is.null(station)){
end_of_season <- data %>%
dplyr::group_by(.data[[station]], .add = TRUE, .drop = FALSE)
dplyr::group_by(.data[[station]], .add = TRUE, .drop = drop)
} else {
end_of_season <- data
}
Expand All @@ -84,7 +85,7 @@ end_season <- function(data, date_time, station = NULL, year = NULL, rain = NULL
dplyr::mutate(wb_max = Reduce(f = function(x, y) pmin(pmax(x + y, 0), capacity), x = utils::tail(x=rain_max - evaporation_value, n=-1), init=0, accumulate=TRUE)) %>%
dplyr::mutate(wb = ifelse((wb_min != wb_max) | is.na(.data[[rain]]), NA, wb_min)) %>%
dplyr::filter(wb <= water_balance_max | is.na(.data[[rain]])) %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = FALSE) %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = drop) %>%
dplyr::filter(.data[[doy]] >= start_day & .data[[doy]] <= end_day, .preserve = TRUE)

if (output == "doy"){
Expand Down
4 changes: 2 additions & 2 deletions R/get_extremes.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ get_extremes <- function(data, station = NULL, year, element, type = c("percenti
}

if (!is.null(station)){
extreme_data <- extreme_data %>% dplyr::group_by(.data[[station]], .drop = FALSE)
extreme_data <- extreme_data %>% dplyr::group_by(.data[[station]])
}
extreme_data[[year]] <- factor(extreme_data[[year]])
extreme_data <- extreme_data %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = FALSE) %>%
dplyr::group_by(.data[[year]], .add = TRUE) %>%
summarise(count = n())

return(extreme_data)
Expand Down
2 changes: 1 addition & 1 deletion R/probability_season_start.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ probability_season_start <- function(data, station = NULL, start_rains, doy_form
if (!is.null(station)){
summary_proportion <- purrr::map_df(.x = specified_day,
.f =~ data %>% dplyr::mutate(LT = ifelse(.data[[start_rains]] < .x, 1, 0)) %>%
dplyr::group_by(.data[[station]], .drop = FALSE) %>%
dplyr::group_by(.data[[station]], .drop = TRUE) %>%
dplyr::summarise(day = .x, proportion = sum(LT, na.rm = TRUE)/dplyr::n()))
} else {
summary_proportion <- purrr::map_df(.x = specified_day,
Expand Down
6 changes: 4 additions & 2 deletions R/start_rains.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @param start_day \code{numerical(1)} The first day to calculate from in the year (1-366).
#' @param end_day \code{numerical(1)} The last day to calculate to in the year (1-366).
#' @param s_start_doy \code{numerical(1)} Default `NULL` (if `NULL`, `s_start_doy = 1`. The day of year to state is the first day of year.
#' @param drop \code{logical(1)} default `TRUE`. Whether to drop years where there are `NA` data for the rainfall.
#' @param output \code{character(1)} Whether to give the start of rains by day of year (doy), date, or both. Default `"doy"`.
#' @param total_rainfall \code{logical(1)} default `TRUE`. Start of the rains to be defined by the total or proportion of rainfall over a period.
#' @param over_days \code{numerical(1)} Only works if `total_rainfall = TRUE`. This is the number of days to total the rainfall over.
Expand All @@ -35,6 +36,7 @@
#' # check against R-Instat function
start_rains <- function(data, date_time, station = NULL, year = NULL, rain = NULL, threshold = 0.85,
doy = NULL, start_day = 1, end_day = 366, s_start_doy = NULL,
drop = TRUE,
output = c("doy", "date", "both"),
total_rainfall = TRUE, over_days = 1, amount_rain = 20, proportion = FALSE, prob_rain_day = 0.8,
number_rain_days = FALSE, min_rain_days = 1, rain_day_interval = 2,
Expand Down Expand Up @@ -107,7 +109,7 @@ start_rains <- function(data, date_time, station = NULL, year = NULL, rain = NUL

if (!is.null(station)){
start_of_rains <- data %>%
dplyr::group_by(.data[[station]], .drop = FALSE)
dplyr::group_by(.data[[station]], .drop = drop)
} else {
start_of_rains <- data
}
Expand Down Expand Up @@ -168,7 +170,7 @@ start_rains <- function(data, date_time, station = NULL, year = NULL, rain = NUL
}

start_of_rains <- start_of_rains %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = FALSE) %>%
dplyr::group_by(.data[[year]], .add = TRUE, .drop = drop) %>%
dplyr::filter(.data[[doy]] >= start_day & .data[[doy]] <= end_day, .preserve = TRUE)

if (output == "doy"){
Expand Down
3 changes: 3 additions & 0 deletions man/end_rains.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/end_season.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/start_rains.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9801eaf

Please sign in to comment.