Skip to content

Commit

Permalink
tabyl on factor with empty levels yields 0s, not NAs
Browse files Browse the repository at this point in the history
closes #48
  • Loading branch information
sfirke committed Aug 18, 2016
1 parent f6958e0 commit 1d14dec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions R/tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ tabyl.default <- function(vec, sort = FALSE, show_na = TRUE, ...) {
# reassign correct variable name
names(result)[1] <- var_name

# replace all NA values with 0 - only applies to missing factor levels
result <- replace_na(result, replace = list(n = 0, percent = 0))


## NA handling:
# if there are NA values & show_na = T, calculate valid % as a new column
if(show_na && sum(is.na(result[[1]])) > 0) {
Expand All @@ -74,6 +78,7 @@ tabyl.default <- function(vec, sort = FALSE, show_na = TRUE, ...) {
dplyr::filter(!is.na(.[1])) %>%
dplyr::mutate(percent = n / sum(n, na.rm = TRUE)) # recalculate % without NAs
}

data.frame(result, check.names = FALSE)
}

Expand Down
25 changes: 14 additions & 11 deletions tests/testthat/test-tabyl.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ test_that("sorting is preserved for factors", {
})

# missing factor levels shown, with and without NA
fac <- factor(c("a"), levels = c("b", "a"))
fac_na <- factor(c("a", NA), levels = c("b", "a"))
fac <- iris[["Species"]][70:80] # to get versicolor, not the first alphabetically
fac_na <- fac
fac_na[1:2] <- NA


test_that("missing factor levels are displayed without NA values", {
expect_equal(tabyl(fac)[[1]], factor(c("b","a"), levels = c("b", "a")))
expect_equal(tabyl(fac)[[2]], c(NA, 1))
expect_equal(tabyl(fac)[[3]], c(NA, 1))
expect_equal(tabyl(fac)[[1]], factor(c("setosa","versicolor", "virginica"), levels = c("setosa", "versicolor", "virginica")))
expect_equal(tabyl(fac)[[2]], c(0, 11, 0))
expect_equal(tabyl(fac)[[3]], c(0, 1, 0))
})

test_that("missing factor levels are displayed with NA values", {
expect_equal(tabyl(fac_na)[[1]], factor(c("b","a", NA), levels = c("b", "a")))
expect_equal(tabyl(fac_na)[[2]], c(NA, 1, 1))
expect_equal(tabyl(fac_na)[[3]], c(NA, 0.5, 0.5))
expect_equal(tabyl(fac_na)[[4]], c(NA, 1, NA))
expect_equal(tabyl(fac_na)[[1]], factor(c("setosa","versicolor", "virginica", NA), levels = c("setosa", "versicolor", "virginica")))
expect_equal(tabyl(fac_na)[[2]], c(0, 9, 0, 2))
expect_equal(tabyl(fac_na)[[3]], c(0, 9/11, 0, 2/11))
expect_equal(tabyl(fac_na)[[4]], c(0, 1, 0, NA))
})

# check sort parameter
Expand All @@ -71,9 +72,11 @@ test_that("sort parameter works", {
expect_equal(sorted_test_df_na[[1]], c("b", "a", "c", NA))
expect_equal(sorted_test_df_na[[4]], c(0.5, 0.25, 0.25, NA))
expect_equal(sorted_with_fac[[1]], factor(c("c", "a", "b"), levels = letters[1:3]))
expect_equal(sorted_with_fac[[2]], c(2, 1, NA))
expect_equal(sorted_with_fac[[2]], c(2, 1, 0))
expect_equal(sorted_with_na_and_fac_res[[1]], factor(c("c", "a", "b", NA), levels = letters[1:3]))
expect_equal(sorted_with_na_and_fac_res[[2]], c(2, 1, NA, 1))
expect_equal(sorted_with_na_and_fac_res[[2]], c(2, 1, 0, 1))
expect_equal(sorted_with_na_and_fac_res[[3]], c(2/4, 1/4, 0, 1/4))
expect_equal(sorted_with_na_and_fac_res[[4]], c(2/3, 1/3, 0, NA))
})

# piping
Expand Down

0 comments on commit 1d14dec

Please sign in to comment.