From a0c26cfab0dd2c5403e0135b43d9939331ab6996 Mon Sep 17 00:00:00 2001 From: lilyclements Date: Wed, 6 Mar 2024 13:24:02 +0000 Subject: [PATCH] Adding join_null_data function --- NAMESPACE | 1 + R/crops_definitions.R | 6 ++---- R/join_null_data.R | 32 ++++++++++++++++++++++++++++++++ man/crops_definitions.Rd | 1 + man/join_null_data.Rd | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 R/join_null_data.R create mode 100644 man/join_null_data.Rd diff --git a/NAMESPACE b/NAMESPACE index 314dd9e..c84468e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(elements_wider) export(end_rains) export(end_season) export(get_extremes) +export(join_null_data) export(mean_temperature) export(null_to_string) export(pentad) diff --git a/R/crops_definitions.R b/R/crops_definitions.R index d0735a2..f17f1da 100644 --- a/R/crops_definitions.R +++ b/R/crops_definitions.R @@ -20,6 +20,7 @@ #' #' @examples #TODO #' +#' crops_definitions <- function (data, date_time, station = NULL, rain, year = NULL, doy = NULL, water_requirements, planting_dates, planting_length, start_check = TRUE, season_data = NULL, start_day, end_day) { @@ -29,14 +30,11 @@ crops_definitions <- function (data, date_time, station = NULL, rain, year = NUL is_station <- !is.null(station) checkmate::assert_data_frame(data) checkmate::assert_data_frame(season_data) - assert_column_names(season_data, start_day) - assert_column_names(season_data, end_day) checkmate::assert_character(rain) checkmate::assert_string(station, null.ok = TRUE) checkmate::assert_string(year, null.ok = TRUE) checkmate::assert_string(doy, null.ok = TRUE) checkmate::assert_logical(start_check, null.ok = TRUE) - assert_column_names(data, rain) checkmate::assert(checkmate::check_date(data[[date_time]], null.ok = TRUE), checkmate::check_posixct(data[[date_time]], null.ok = TRUE)) @@ -121,4 +119,4 @@ crops_definitions <- function (data, date_time, station = NULL, rain, year = NUL na.rm = TRUE)/length(stats::na.omit(overall_cond))) df$prop_success <- round(df$prop_success, 2) return(df) -} \ No newline at end of file +} diff --git a/R/join_null_data.R b/R/join_null_data.R new file mode 100644 index 0000000..5e8b58e --- /dev/null +++ b/R/join_null_data.R @@ -0,0 +1,32 @@ +#' Join Null Data +#' +#' This function joins two data frames, `summary_data` and `calculated_data`, +#' using a full join if `summary_data` is not NULL. If `summary_data` is NULL, +#' it assigns `calculated_data` to `summary_data`. +#' +#' @param summary_data A data frame representing summary data. +#' @param calculated_data A data frame containing calculated data. +#' +#' @return A data frame resulting from the full join of `summary_data` and `calculated_data`, +#' or `calculated_data` if `summary_data` is NULL. +#' +#' @export +#' +#' @examples +#' # summary_data is NULL +#' summary_data <- NULL +#' calculated_data <- data.frame(x = 1:5, y = letters[1:5]) +#' join_null_data(summary_data, calculated_data) +#' +#' # summary_data is not NULL +#' summary_data <- data.frame(x = 1:3, y = letters[1:3]) +#' calculated_data <- data.frame(x = 4:5, y = letters[4:5]) +#' join_null_data(summary_data, calculated_data) +join_null_data <- function(summary_data, calculated_data){ + if (is.null(summary_data)){ + summary_data <- calculated_data + } else{ + summary_data <- dplyr::full_join(summary_data, calculated_data) + } + return(summary_data) +} diff --git a/man/crops_definitions.Rd b/man/crops_definitions.Rd index 193ef6b..7c52fe4 100644 --- a/man/crops_definitions.Rd +++ b/man/crops_definitions.Rd @@ -56,4 +56,5 @@ Calculate the probabilities of crop success for given planting maturity lengths, \examples{ #TODO + } diff --git a/man/join_null_data.Rd b/man/join_null_data.Rd new file mode 100644 index 0000000..195f029 --- /dev/null +++ b/man/join_null_data.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/join_null_data.R +\name{join_null_data} +\alias{join_null_data} +\title{Join Null Data} +\usage{ +join_null_data(summary_data, calculated_data) +} +\arguments{ +\item{summary_data}{A data frame representing summary data.} + +\item{calculated_data}{A data frame containing calculated data.} +} +\value{ +A data frame resulting from the full join of \code{summary_data} and \code{calculated_data}, +or \code{calculated_data} if \code{summary_data} is NULL. +} +\description{ +This function joins two data frames, \code{summary_data} and \code{calculated_data}, +using a full join if \code{summary_data} is not NULL. If \code{summary_data} is NULL, +it assigns \code{calculated_data} to \code{summary_data}. +} +\examples{ +# summary_data is NULL +summary_data <- NULL +calculated_data <- data.frame(x = 1:5, y = letters[1:5]) +join_null_data(summary_data, calculated_data) + +# summary_data is not NULL +summary_data <- data.frame(x = 1:3, y = letters[1:3]) +calculated_data <- data.frame(x = 4:5, y = letters[4:5]) +join_null_data(summary_data, calculated_data) +}