Skip to content

Commit

Permalink
V2 fix for all character vectors (#423)
Browse files Browse the repository at this point in the history
* Update news.

* min_char() and max_char() should return NA when all values are NA.
  • Loading branch information
elinw authored Mar 23, 2019
1 parent ec80a4c commit fd0bb47
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ skimr 1.0.6 (xxx-xx-xx)
=======================
### BUG FIXES
* Fix issue where skim_tee() was not respecting ... options.
* Fix issue where all NA character vectors were not returning NA for
max() and min()

skimr 1.0.5 (2019-01-05)
========================
Expand Down
2 changes: 2 additions & 0 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ n_empty <- function(x) {
#' character vector.
#' @export
min_char <- function(x) {
if(all(is.na(x))) return(NA)
characters <- nchar(x, allowNA = TRUE)
min(characters, na.rm = TRUE)
}
Expand All @@ -159,6 +160,7 @@ min_char <- function(x) {
#' character vector.
#' @export
max_char <- function(x) {
if(all(is.na(x))) return(NA)
characters <- nchar(x, allowNA = TRUE)
max(characters, na.rm = TRUE)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ test_that("min_char with a multibyte character does not throw an error.", {
expect_error(min_char(data), NA)
})

test_that("min_char returns NA when there are only NA values.",{
data <- as.character(c(NA, NA, NA, NA))
input <- min_char(data)
expect_equal(input, NA)
})

test_that("max_char is calculated correctly, including empty strings.", {
data <- c("a", "ab", "abc", "")
correct <- as.integer(3)
Expand All @@ -136,6 +142,12 @@ test_that("max_char with a multibyte character does not throw an error.", {
expect_error(max_char(data, NA))
})

test_that("max_char returns NA when there are only NA values.",{
data <- as.character(c(NA, NA, NA, NA))
input <- max_char(data)
expect_equal(input, NA)
})

test_that("n_unique is calculated correctly.", {
correct <- 4L
data <- c("a", "ab", "abc", "")
Expand Down

0 comments on commit fd0bb47

Please sign in to comment.