Skip to content

Commit

Permalink
Add custom filter value to clean_ending function
Browse files Browse the repository at this point in the history
Enhanced the clean_ending function by introducing an `ending_value` parameter, allowing users to specify a custom value for filtering the "Ending" variable, in addition to the existing functionality of handling NA values and the default "Normal" filter. Updated the function's documentation to reflect this new feature and provided examples demonstrating its usage for various scenarios.
  • Loading branch information
CharlesEtienneLavoie committed Feb 15, 2024
1 parent 3374ca8 commit 1fa37f4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
39 changes: 21 additions & 18 deletions R/clean_ending.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
#'
#' @description `clean_ending` function is designed to filter a dataset
#' based on the status of the "Ending" variable. It allows users to either
#' include or exclude observations where the Ending is NA or explicitly marked
#' as "Normal". This function provides a simple yet effective method for focusing
#' analyses on subsets of data that meet specific criteria related to their
#' conclusion status.
#' include or exclude observations where the Ending is NA or matches a specific
#' value, which by default is "Normal". This function provides a simple yet
#' effective method for focusing analyses on subsets of data that meet specific
#' criteria related to their conclusion status.
#'
#' @param data The dataset to be filtered.
#' @param ending_value The value of the "Ending" variable to filter the dataset by.
#' The default value is "Normal", indicating that by default, observations with
#' an Ending of "Normal" or NA (if keep_na is TRUE) are kept.
#' @param keep_na Logical value indicating whether to keep observations where
#' the Ending is NA. If TRUE, observations where Ending is NA or "Normal" are kept.
#' If FALSE, only observations where Ending is "Normal" are kept (default is TRUE).
#' the Ending is NA alongside the specified `ending_value`. If TRUE, observations
#' where Ending is NA or matches the `ending_value` are kept. If FALSE, only
#' observations where Ending matches the `ending_value` are kept. The default is TRUE.
#'
#' @return A filtered dataset based on the specified criteria for the Ending status.
#' Additionally, it reports the number of observations before and after filtering,
#' as well as the number of observations filtered out.
#' as well as the number of observations filtered out based on the ending scenario filter.
#'
#' @references Add references here.
#' @export
Expand All @@ -30,27 +34,26 @@
#'
#' # Filter the dataset to keep only "Normal" endings, excluding NAs
#' filtered_df_no_na <- clean_ending(df, keep_na = FALSE)
#'
#' # Filter the dataset for a different ending value, say "Critical", including NAs
#' filtered_df_critical <- clean_ending(df, ending_value = "Critical", keep_na = TRUE)
#'
#' # Filter the dataset for a different ending value, say "Abnormal", excluding NAs
#' filtered_df_abnormal <- clean_ending(df, ending_value = "Abnormal", keep_na = FALSE)
#' }

clean_ending <- function(data, keep_na = TRUE) {
clean_ending <- function(data, ending_value = "Normal", keep_na = TRUE) {
if (keep_na) {
# Filter to keep rows where Ending is NA or "Normal"
filtered_data <- data %>%
filter(is.na(Ending) | Ending == "Normal")
filtered_data <- data %>% filter(is.na(Ending) | Ending == ending_value)
} else {
# Filter to keep rows where Ending is "Normal", excluding NAs
filtered_data <- data %>%
filter(Ending == "Normal")
filtered_data <- data %>% filter(Ending == ending_value)
}

# Report the number of observations filtered
n_before <- nrow(data)
n_after <- nrow(filtered_data)
n_filtered <- n_before - n_after

cat("Number of observations before filtering:", n_before, "\n")
cat("Number of observations after filtering:", n_after, "\n")
cat("Number of observations filtered out based on the ending scenario filter:", n_filtered, "\n")

return(filtered_data)
}

27 changes: 19 additions & 8 deletions man/clean_ending.Rd

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

0 comments on commit 1fa37f4

Please sign in to comment.