-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added widen_model_data to clean up process
- Loading branch information
William Augustine McLean
authored and
William Augustine McLean
committed
Oct 11, 2024
1 parent
d752f88
commit 8403c5a
Showing
7 changed files
with
73 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Widen Model Data and Calculate Error | ||
#' | ||
#' This function processes the output of a cleaned model dataset by pivoting the data to a wide format and calculating the error (ERR) based on available variables. The error is calculated as the difference between `ppt`, `aet`, and other relevant variables such as `rch`, `run`, and `str`. | ||
#' | ||
#' @param data A data frame containing model output. The data should have columns for `var`, `value`, `ppt`, `aet`, `run`, `str`, and optionally `rch`. | ||
#' | ||
#' @return A data frame in wide format with an additional column `ERR`, representing the calculated error. | ||
#' | ||
#' @details | ||
#' The function performs the following steps: | ||
#' \itemize{ | ||
#' \item **Pivot Wider**: Converts the data to wide format with separate columns for each variable. | ||
#' \item **Error Calculation**: Calculates the error (`ERR`) as `ppt - aet - rch - run - str` when `rch` is available, or `ppt - aet - run - str` when it's not. | ||
#' \item **Filter**: Filters out rows where `divide_id` is equal to `"cat-351"`. | ||
#' } | ||
#' | ||
#' @note Ensure that the input data contains the required columns: `var`, `value`, `ppt`, `aet`, `run`, `str`, and optionally `rch`. | ||
#' | ||
#' @export | ||
|
||
|
||
widen_model_data <- function(data) { | ||
|
||
|
||
var <- value <- ppt <- aet <- rch <- run <- str <- divide_id <- ERR <- NULL | ||
|
||
# Step 1: Pivot wider and calculate ERR | ||
data_wide <- data %>% | ||
pivot_wider(names_from = var, values_from = value) | ||
|
||
if ("rch" %in% names(data_wide)) { | ||
data_wide <- data_wide %>% | ||
mutate(ERR = ppt - aet - rch - run - str) %>% | ||
filter(divide_id != "cat-351") | ||
} else { | ||
data_wide <- data_wide %>% | ||
mutate(ERR = ppt - aet - run - str) | ||
} | ||
# Return a list of data frames for each season | ||
return(data_wide) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.