Skip to content

Commit

Permalink
rename arg
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 10, 2024
1 parent f820ffa commit 3a9b8a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
24 changes: 12 additions & 12 deletions R/row_count.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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]
Expand Down
16 changes: 8 additions & 8 deletions man/row_count.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-row_count.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ 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"),
c3 = c(NA, 4, NA, NA),
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))
})

0 comments on commit 3a9b8a6

Please sign in to comment.