-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dff2ae
commit 1dcec60
Showing
7 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Type: Package | ||
Package: datawizard | ||
Title: Easy Data Wrangling and Statistical Transformations | ||
Version: 0.13.0.2 | ||
Version: 0.13.0.4 | ||
Authors@R: c( | ||
person("Indrajeet", "Patil", , "[email protected]", role = "aut", | ||
comment = c(ORCID = "0000-0003-1995-6531")), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' @title Row means or sums (optionally with minimum amount of valid values) | ||
#' @name row_count | ||
#' @description `row_count()` mimics base R's `rowSums()`, with sums for a | ||
#' specific value indicated by `count`. Hence, it is equivalent to | ||
#' `rowSums(x == count, na.rm = 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`. | ||
#' @inheritParams extract_column_names | ||
#' @inheritParams row_means | ||
#' | ||
#' @return A vector with row-wise counts of values specified in `count`. | ||
#' | ||
#' @examples | ||
#' dat <- data.frame( | ||
#' c1 = c(1, 2, NA, 4), | ||
#' c2 = c(NA, 2, NA, 5), | ||
#' c3 = c(NA, 4, NA, NA), | ||
#' c4 = c(2, 3, 7, 8) | ||
#' ) | ||
#' | ||
#' # count all 2s per row | ||
#' row_count(dat, count = 2) | ||
#' # count all missing values per row | ||
#' row_count(dat, count = NA) | ||
#' | ||
#' @export | ||
row_count <- function(data, | ||
select = NULL, | ||
exclude = NULL, | ||
count = NULL, | ||
ignore_case = FALSE, | ||
regex = FALSE, | ||
verbose = TRUE) { | ||
# evaluate arguments | ||
select <- .select_nse(select, | ||
data, | ||
exclude, | ||
ignore_case = ignore_case, | ||
regex = regex, | ||
verbose = verbose | ||
) | ||
|
||
if (is.null(count)) { | ||
insight::format_error("`count` must be a valid value (including `NA` or `Inf`), but not `NULL`.") | ||
} | ||
|
||
if (is.null(select) || length(select) == 0) { | ||
insight::format_error("No columns selected.") | ||
} | ||
|
||
data <- .coerce_to_dataframe(data[select]) | ||
|
||
# check if we have a data framme with at least two columns | ||
if (ncol(data) < 2) { | ||
insight::format_error("`data` must be a data frame with at least two numeric columns.") | ||
} | ||
|
||
# special case: count missing | ||
if (is.na(count)) { | ||
rowSums(is.na(data)) | ||
} else { | ||
rowSums(data == count, na.rm = TRUE) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ reference: | |
- kurtosis | ||
- smoothness | ||
- skewness | ||
- row_count | ||
- row_means | ||
- weighted_mean | ||
- mean_sd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
test_that("row_count", { | ||
d_mn <- data.frame( | ||
c1 = c(1, 2, NA, 4), | ||
c2 = c(NA, 2, NA, 5), | ||
c3 = c(NA, 4, NA, NA), | ||
c4 = c(2, 3, 7, 8) | ||
) | ||
expect_identical(row_count(d_mn, count = 2), c(1, 2, 0, 0)) | ||
expect_identical(row_count(d_mn, count = NA), c(2, 0, 3, 1)) | ||
d_mn <- data.frame( | ||
Check warning on line 10 in tests/testthat/test-row_count.R GitHub Actions / lint-changed-files / lint-changed-files
|
||
c1 = c("a", "b", NA, "c"), | ||
c2 = c(NA, "b", NA, "d"), | ||
c3 = c(NA, 4, NA, NA), | ||
c4 = c(2, 3, 7, Inf) | ||
) | ||
expect_identical(row_count(d_mn, count = "b"), c(0, 2, 0, 0)) | ||
expect_identical(row_count(d_mn, count = Inf), c(0, 0, 0, 1)) | ||
}) | ||
|
||
test_that("row_means, errors or messages", { | ||
data(iris) | ||
expect_error(expect_warning(row_count(iris, select = "abc")), regex = "must be a valid") | ||
expect_error(expect_warning(row_count(iris, select = "abc", count = 3)), regex = "no columns") | ||
expect_error(row_count(iris[1], count = 3), regex = "with at least") | ||
}) |