Skip to content

Commit

Permalink
Adding join_null_data function
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Mar 6, 2024
1 parent 900e463 commit a0c26cf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions R/crops_definitions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}
}
32 changes: 32 additions & 0 deletions R/join_null_data.R
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions man/crops_definitions.Rd

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

33 changes: 33 additions & 0 deletions man/join_null_data.Rd

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

0 comments on commit a0c26cf

Please sign in to comment.