From f34e78ccb5e129a59e33ab07ad1eefa12b156fc2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Feb 2024 14:59:37 +0100 Subject: [PATCH] add tests --- R/data_tabulate.R | 2 +- man/data_tabulate.Rd | 2 +- tests/testthat/test-data_tabulate.R | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/R/data_tabulate.R b/R/data_tabulate.R index fc2374cef..02f0effff 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -10,7 +10,7 @@ #' @param by Optional vector or factor. If supplied, a crosstable is created. #' If `x` is a data frame, `by` can also be a character string indicating the #' name of a variable in `x`. -#' @param drop_levels Logical, if `TRUE`, factor levels that do not occur in +#' @param drop_levels Logical, if `FALSE`, factor levels that do not occur in #' the data are included in the table (with frequency of zero), else unused #' factor levels are dropped from the frequency table. #' @param name Optional character string, which includes the name that is used diff --git a/man/data_tabulate.Rd b/man/data_tabulate.Rd index 55f7d316c..effcd2b22 100644 --- a/man/data_tabulate.Rd +++ b/man/data_tabulate.Rd @@ -45,7 +45,7 @@ data_tabulate(x, ...) If \code{x} is a data frame, \code{by} can also be a character string indicating the name of a variable in \code{x}.} -\item{drop_levels}{Logical, if \code{TRUE}, factor levels that do not occur in +\item{drop_levels}{Logical, if \code{FALSE}, factor levels that do not occur in the data are included in the table (with frequency of zero), else unused factor levels are dropped from the frequency table.} diff --git a/tests/testthat/test-data_tabulate.R b/tests/testthat/test-data_tabulate.R index 7ed76e7c9..9a24252b0 100644 --- a/tests/testthat/test-data_tabulate.R +++ b/tests/testthat/test-data_tabulate.R @@ -243,6 +243,16 @@ test_that("data_tabulate print, collapse groups, drop levels", { ) }) +test_that("data_tabulate drop levels", { + x <- factor(rep(letters[1:3], 3), levels = letters[1:5]) + out1 <- data_tabulate(x, drop_levels = FALSE) + out2 <- data_tabulate(x, drop_levels = TRUE) + expect_identical(out1$N, c(3L, 3L, 3L, 0L, 0L, 0L)) + expect_identical(as.character(out1$Value), c("a", "b", "c", "d", "e", NA)) + expect_identical(out2$N, c(3L, 3L, 3L, 0L)) + expect_identical(as.character(out2$Value), c("a", "b", "c", NA)) +}) + # select helpers ------------------------------ test_that("data_tabulate regex", { @@ -254,6 +264,8 @@ test_that("data_tabulate regex", { }) +# missing values ------------------------------ + test_that("data_tabulate exclude/include missing values", { data(efc, package = "datawizard") set.seed(123)