Skip to content

Commit

Permalink
Closes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-Gon committed Sep 17, 2023
1 parent fa92e7e commit 3bdab07
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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..")

}
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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")


Expand All @@ -131,23 +131,23 @@ 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
}

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."))

}
Expand All @@ -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))

Expand Down Expand Up @@ -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")
Expand All @@ -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


2 changes: 1 addition & 1 deletion man/all_na.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/na_counts.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/recode_selectors.Rd

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

17 changes: 11 additions & 6 deletions tests/testthat/test_get_na_counts.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)



})

0 comments on commit 3bdab07

Please sign in to comment.