Skip to content

Commit

Permalink
Merge pull request #22 from gbganalyst/input-checking
Browse files Browse the repository at this point in the history
Input checking
  • Loading branch information
gbganalyst authored Apr 11, 2024
2 parents 98c6aa7 + f39db9e commit e6cfa10
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ VignetteBuilder:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
39 changes: 22 additions & 17 deletions R/fill_missing_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#' which missing values should be imputed. If `NULL` (default), imputation is
#' applied to all variables in the data frame.
#'
#' @param method A character string specifying the imputation method for continuous
#' variables. Supported methods are "min", "max", "mean", "median", "harmonic",
#' and "geometric". The default method is "mean". For categorical variables, the
#' mode is always used.
#' @param method A character string specifying the imputation method for
#' continuous variables. Supported methods are "min", "max", "mean", "median",
#' "harmonic", and "geometric". The default method is "mean". For categorical
#' variables, the mode is always used.
#'
#' @return A data frame with missing values imputed according to the specified `method`.
#' @return A data frame with missing values imputed according to the specified
#' `method`.
#'
#' @export
#'
Expand Down Expand Up @@ -71,17 +72,21 @@
#' map_df(fill_missing_values, method = "median")
#'
#'
fill_missing_values <- function(df, selected_variables = NULL, method = "mean") {
fill_missing_values <- function(df, selected_variables = NULL, method = c("mean", "min", "max", "median", "harmonic", "geometric")) {

# Check if df is a dataframe

if (!is.data.frame(df)) {
stop("fill_missing_values() is designed to operate exclusively on objects of the class 'dataframe'")
}

if (missing(df)) {
stop("argument 'df' is missing, with no default")
}

# Validate method input for continuous variables
valid_methods <- c("min", "max", "mean", "median", "harmonic", "geometric")
if (!(method %in% valid_methods)) {
stop("Invalid method. Choose from 'min', 'max', 'mean', 'median', 'harmonic', 'geometric'")
}

method <- rlang::arg_match(method)

# Calculate the replacement value based on the specified method

Expand All @@ -91,13 +96,13 @@ fill_missing_values <- function(df, selected_variables = NULL, method = "mean")
} # Skip non-numeric columns

replacement_value <- switch(method,
min = min(x, na.rm = TRUE),
max = max(x, na.rm = TRUE),
mean = mean(x, na.rm = TRUE),
median = median(x, na.rm = TRUE),
harmonic = harmonic_mean(x),
geometric = geometric_mean(x),
x
min = min(x, na.rm = TRUE),
max = max(x, na.rm = TRUE),
mean = mean(x, na.rm = TRUE),
median = median(x, na.rm = TRUE),
harmonic = harmonic_mean(x),
geometric = geometric_mean(x),
x
) # Default to return x as is


Expand Down
6 changes: 4 additions & 2 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#' Pipe operator
#'
#' See \code{magrittr::\link[magrittr]{\%>\%}} for details.
#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @return No return value
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
17 changes: 11 additions & 6 deletions man/fill_missing_values.Rd

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

9 changes: 7 additions & 2 deletions man/pipe.Rd

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

2 changes: 1 addition & 1 deletion man/read_csv_files_from_dir.Rd

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

2 changes: 1 addition & 1 deletion vignettes/other-functions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ airquality %>%

## fill_missing_values()

`fill_missing_values()` is an efficient function that addresses missing values in a data frame. It uses imputation by function, also known as column-based imputation, to impute the missing values. It supports various imputation methods for continuous variables, including minimum, maximum, mean, median, harmonic mean, and geometric mean. For categorical variables, missing values are replaced with the mode of the column. This approach ensures accurate and consistent replacements derived from individual columns, resulting in a complete and reliable dataset for improved analysis and decision-making.
`fill_missing_values()` is an efficient function that addresses missing values in a data frame. It uses imputation by function, also known as column-based imputation, to impute the missing values. It supports various imputation methods for continuous variables, including `minimum`, `maximum`, `mean`, `median`, `harmonic mean`, and `geometric mean`. For categorical variables, missing values are replaced with the `mode` of the column. This approach ensures accurate and consistent replacements derived from individual columns, resulting in a complete and reliable dataset for improved analysis and decision-making.


```{r example 6}
Expand Down

0 comments on commit e6cfa10

Please sign in to comment.