diff --git a/R/row_count.R b/R/row_count.R index d63596472..93f1ebbca 100644 --- a/R/row_count.R +++ b/R/row_count.R @@ -5,18 +5,18 @@ #' `rowSums(x == count, na.rm = TRUE)`, but offers some more options, including #' strict comparisons. Comparisons using `==` coerce values to atomic vectors, #' thus both `2 == 2` and `"2" == 2` are `TRUE`. In `row_count()`, it is also -#' possible to make "type safe" comparisons using the `exact` argument, where -#' `"2" == 2` is not true. +#' possible to make "type safe" comparisons using the `allow_coercion` argument, +#' where `"2" == 2` is not true. #' #' @param data A data frame with at least two columns, where number of specific #' values are counted row-wise. #' @param count The value for which the row sum should be computed. May be a #' numeric value, a character string (for factors or character vectors), `NA` or #' `Inf`. -#' @param exact Logical. If `TRUE`, `count` matches only values of same type -#' (i.e. when `count = 2`, the value `"2"` is not counted and vice versa). -#' By default, when `exact = FALSE`, `count = 2` also matches `"2"`. See -#' 'Examples'. +#' @param allow_coercion Logical. If `TRUE`, `count` matches only values of same +#' type (i.e. when `count = 2`, the value `"2"` is not counted and vice versa). +#' By default, when `allow_coercion = FALSE`, `count = 2` also matches `"2"`. +#' See 'Examples'. #' #' @inheritParams extract_column_names #' @inheritParams row_means @@ -45,14 +45,14 @@ #' # count all 2s and "2"s per row #' row_count(dat, count = 2) #' # only count 2s, but not "2"s -#' row_count(dat, count = 2, exact = TRUE) +#' row_count(dat, count = 2, allow_coercion = TRUE) #' #' @export row_count <- function(data, select = NULL, exclude = NULL, count = NULL, - exact = FALSE, + allow_coercion = FALSE, ignore_case = FALSE, regex = FALSE, verbose = TRUE) { @@ -90,10 +90,10 @@ row_count <- function(data, rowSums(is.na(data)) } else { # comparisons in R using == coerce values into a atomic vector, i.e. - # 2 == "2" is TRUE. If `exact = TRUE`, we only want 2 == 2 or "2" == "2". - # to achieve this, we simply compute the comparison on numeric or non-numeric - # columns only - if (isTRUE(exact)) { + # 2 == "2" is TRUE. If `allow_coercion = TRUE`, we only want 2 == 2 or + # "2" == "2". to achieve this, we simply compute the comparison on numeric + # or non-numeric columns only + if (isTRUE(allow_coercion)) { numeric_columns <- vapply(data, is.numeric, TRUE) if (is.numeric(count)) { data <- data[numeric_columns] diff --git a/man/row_count.Rd b/man/row_count.Rd index c6a2bbdbc..cfcb051f1 100644 --- a/man/row_count.Rd +++ b/man/row_count.Rd @@ -9,7 +9,7 @@ row_count( select = NULL, exclude = NULL, count = NULL, - exact = FALSE, + allow_coercion = FALSE, ignore_case = FALSE, regex = FALSE, verbose = TRUE @@ -61,10 +61,10 @@ excludes no columns.} numeric value, a character string (for factors or character vectors), \code{NA} or \code{Inf}.} -\item{exact}{Logical. If \code{TRUE}, \code{count} matches only values of same type -(i.e. when \code{count = 2}, the value \code{"2"} is not counted and vice versa). -By default, when \code{exact = FALSE}, \code{count = 2} also matches \code{"2"}. See -'Examples'.} +\item{allow_coercion}{Logical. If \code{TRUE}, \code{count} matches only values of same +type (i.e. when \code{count = 2}, the value \code{"2"} is not counted and vice versa). +By default, when \code{allow_coercion = FALSE}, \code{count = 2} also matches \code{"2"}. +See 'Examples'.} \item{ignore_case}{Logical, if \code{TRUE} and when one of the select-helpers or a regular expression is used in \code{select}, ignores lower/upper case in the @@ -90,8 +90,8 @@ specific value indicated by \code{count}. Hence, it is equivalent to \code{rowSums(x == count, na.rm = TRUE)}, but offers some more options, including strict comparisons. Comparisons using \code{==} coerce values to atomic vectors, thus both \code{2 == 2} and \code{"2" == 2} are \code{TRUE}. In \code{row_count()}, it is also -possible to make "type safe" comparisons using the \code{exact} argument, where -\code{"2" == 2} is not true. +possible to make "type safe" comparisons using the \code{allow_coercion} argument, +where \code{"2" == 2} is not true. } \examples{ dat <- data.frame( @@ -115,6 +115,6 @@ dat <- data.frame( # count all 2s and "2"s per row row_count(dat, count = 2) # only count 2s, but not "2"s -row_count(dat, count = 2, exact = TRUE) +row_count(dat, count = 2, allow_coercion = TRUE) } diff --git a/tests/testthat/test-row_count.R b/tests/testthat/test-row_count.R index 7eea5d6a6..57c7e76ed 100644 --- a/tests/testthat/test-row_count.R +++ b/tests/testthat/test-row_count.R @@ -26,7 +26,7 @@ test_that("row_count, errors or messages", { expect_error(row_count(iris[-seq_len(nrow(iris)), , drop = FALSE], count = 2), regex = "one row") }) -test_that("row_count, exact match", { +test_that("row_count, allow_coercion match", { d_mn <- data.frame( c1 = c("1", "2", NA, "3"), c2 = c(NA, "2", NA, "3"), @@ -34,7 +34,7 @@ test_that("row_count, exact match", { c4 = c(2, 3, 7, Inf), stringsAsFactors = FALSE ) - expect_identical(row_count(d_mn, count = 2, exact = FALSE), c(1, 2, 0, 0)) - expect_identical(row_count(d_mn, count = 2, exact = TRUE), c(1, 0, 0, 0)) - expect_identical(row_count(d_mn, count = "2", exact = TRUE), c(0, 2, 0, 0)) + expect_identical(row_count(d_mn, count = 2, allow_coercion = FALSE), c(1, 2, 0, 0)) + expect_identical(row_count(d_mn, count = 2, allow_coercion = TRUE), c(1, 0, 0, 0)) + expect_identical(row_count(d_mn, count = "2", allow_coercion = TRUE), c(0, 2, 0, 0)) })