diff --git a/DESCRIPTION b/DESCRIPTION index fe9a2d9..14db687 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: mde Title: Missing Data Explorer -Version: 0.3.2 +Version: 0.3.3.9000 Authors@R: person(given = "Nelson", family = "Gonzabato", @@ -16,7 +16,7 @@ Imports: dplyr(>= 1.0.0), tidyr(>= 1.0.3) Encoding: UTF-8 -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 URL: https://github.com/Nelson-Gon/mde BugReports: https://github.com/Nelson-Gon/mde/issues Suggests: diff --git a/NAMESPACE b/NAMESPACE index 4f7c185..b77ce4f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,6 +19,7 @@ S3method(get_na_means,numeric) S3method(na_counts,POSIXct) S3method(na_counts,character) S3method(na_counts,factor) +S3method(na_counts,logical) S3method(na_counts,numeric) S3method(na_summary,data.frame) S3method(percent_missing,data.frame) diff --git a/NEWS.md b/NEWS.md index 6140609..055b31f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,10 @@ date: "`r Sys.Date()`" output: html_document --- +# mde 0.3.3 + +* Fixed a bug that caused missingness counts to fail in case of logical vectors. + # mde 0.3.2 * There is now a function `recode_as_value` for general recoding. See diff --git a/R/helpers.R b/R/helpers.R index 60c201f..abab508 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -9,7 +9,7 @@ utils::globalVariables(c("all_of","metric","value","name", ":=")) #' @title Helper functions in package mde #' @inheritParams recode_na_as #' @param x data.frame object -#' @param column_check If TRUE, pattern search is performed columnwise. +#' @param column_check If TRUE, pattern search is performed columnwise. #' Defaults to FALSE. #' @export recode_selectors <- function(x,column_check=TRUE, @@ -19,11 +19,11 @@ recode_selectors <- function(x,column_check=TRUE, if (!is.null(pattern_type)) { - + if (!pattern_type %in% c("starts_with","ends_with","contains","regex")){ stop("pattern_type should be one of starts_with,ends_with,contains or regex") } - + if(is.null(pattern)) stop("Both a pattern type and pattern should be provided..") } @@ -58,9 +58,9 @@ use_pattern <- switch(pattern_type, #' @export recode_helper <- function(x,pattern_type=NULL,pattern=NULL, - original_value, + original_value, new_value,case_sensitive=FALSE,...){ - + x %>% mutate(across(recode_selectors(x,column_check=TRUE, pattern=pattern, @@ -73,7 +73,7 @@ x %>% #' Checks that all values are NA #' @param x A vector or data.frame column -#' @description This is a helper function to check if all column/vector values +#' @description This is a helper function to check if all column/vector values #' are NA #' @return Boolean TRUE or FALSE depending on the nature of the column/vector #' @examples @@ -113,7 +113,7 @@ skip_on_oldrel <- function(version="3.6.3", msg = NULL) { #' @param x A vector whose mean NA is required. #' @param as_percent Boolean? Report means as percents, defaults to TRUE. #' @examples get_na_means(airquality) -#' @export +#' @export get_na_means <- function(x, as_percent=TRUE) UseMethod("get_na_means") @@ -131,15 +131,15 @@ get_na_means.character <- get_na_means.numeric #' @export get_na_means.factor <- get_na_means.numeric #' @export -get_na_means.POSIXct <- get_na_means.numeric +get_na_means.POSIXct <- get_na_means.numeric #' @export get_na_means.data.frame <- function(x, as_percent=TRUE){ - + res <- colMeans(is.na(x)) - + if(as_percent) res <- res * 100 - + res } @@ -147,7 +147,7 @@ check_column_existence <- function(df, target_columns=NULL, unique_name=NULL){ if(!all(target_columns %in% names(df))){ - + stop(paste0("All columns ", unique_name, " should exist in the data set.")) } @@ -162,7 +162,7 @@ switches.data.frame <- function(target_value=NULL,sign, percent_na = 50){ available_options <- c("gteq","lteq","gt","lt","eq") -if(! sign %in% available_options ) { +if(! sign %in% available_options ) { stop(paste(paste(c("I was expecting one of ", available_options),collapse=" "),"not",sign)) @@ -199,15 +199,15 @@ switches.double <- switches.numeric unexpected_argument <- function(arg, acceptable_values){ if(!arg %in% acceptable_values){ - stop(paste0("Use either ",acceptable_values[1], " or ", + stop(paste0("Use either ",acceptable_values[1], " or ", acceptable_values[2]," not ", arg)) } } #' Get NA counts for a given character, numeric, factor, etc. #' @param x A vector whose number of missing values is to be determined. -#' @examples -#' na_counts(airquality$Ozone) +#' @examples +#' na_counts(airquality$Ozone) #' @export na_counts <- function(x) UseMethod("na_counts") @@ -222,10 +222,14 @@ na_counts.character <- function(x) sum(is.na(x)) #' @export +na_counts.logical <- na_counts.numeric + +#' @export + na_counts.factor <- na_counts.numeric #' @export -na_counts.POSIXct <- na_counts.numeric +na_counts.POSIXct <- na_counts.numeric diff --git a/man/all_na.Rd b/man/all_na.Rd index 71013aa..e837d1e 100644 --- a/man/all_na.Rd +++ b/man/all_na.Rd @@ -13,7 +13,7 @@ all_na(x) Boolean TRUE or FALSE depending on the nature of the column/vector } \description{ -This is a helper function to check if all column/vector values +This is a helper function to check if all column/vector values are NA } \examples{ diff --git a/man/na_counts.Rd b/man/na_counts.Rd index 49cd83e..15f1af4 100644 --- a/man/na_counts.Rd +++ b/man/na_counts.Rd @@ -13,5 +13,5 @@ na_counts(x) Get NA counts for a given character, numeric, factor, etc. } \examples{ -na_counts(airquality$Ozone) +na_counts(airquality$Ozone) } diff --git a/man/recode_selectors.Rd b/man/recode_selectors.Rd index 37168a7..f3a786e 100644 --- a/man/recode_selectors.Rd +++ b/man/recode_selectors.Rd @@ -16,7 +16,7 @@ recode_selectors( \arguments{ \item{x}{data.frame object} -\item{column_check}{If TRUE, pattern search is performed columnwise. +\item{column_check}{If TRUE, pattern search is performed columnwise. Defaults to FALSE.} \item{pattern_type}{One of contains', 'starts_with' or 'ends_with'.} diff --git a/tests/testthat/test_get_na_counts.R b/tests/testthat/test_get_na_counts.R index 4ec2bb4..8fb212c 100644 --- a/tests/testthat/test_get_na_counts.R +++ b/tests/testthat/test_get_na_counts.R @@ -5,21 +5,26 @@ test_that(desc = "get_na_counts works as expected", expect_error(get_na_counts(as.POSIXct(Sys.Date()))) expect_error(get_na_counts(airquality, grouping_cols = "Nope")) - - + + }) - + expect_equal(nrow(get_na_counts(iris, "Species")), 3) - + expect_equal(nrow(get_na_counts(airquality, grouping_cols = "Month")), 5) - + # If using na_counts, expect that we get the same value as # when calling via get_na_counts, using Ozone as an example # here. expect_equal(get_na_counts(airquality)[[1]], na_counts(airquality$Ozone)) - + # Check that no error is thrown if a data set contains logical vectors + ydupe <- as.logical(c("T", "F", "F", "F", "F", "T", NA)) + expect_equal(na_counts(ydupe), 1) + + + })