row_count | R Documentation |
row_count()
mimics base R's rowSums()
, with sums for a
specific value indicated by count
. Hence, it is similar to
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 allow_coercion
argument,
where "2" == 2
is not true.
row_count(
data,
select = NULL,
exclude = NULL,
count = NULL,
allow_coercion = TRUE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE
)
data |
A data frame with at least two columns, where number of specific values are counted row-wise. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
count |
The value for which the row sum should be computed. May be a
numeric value, a character string (for factors or character vectors), |
allow_coercion |
Logical. If |
ignore_case |
Logical, if |
regex |
Logical, if |
verbose |
Toggle warnings. |
A vector with row-wise counts of values specified in count
.
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 4s per row
row_count(dat, count = 4)
# count all missing values per row
row_count(dat, count = NA)
dat <- 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)
)
# 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, allow_coercion = FALSE)
dat <- data.frame(
c1 = factor(c("1", "2", NA, "3")),
c2 = c("2", "1", NA, "3"),
c3 = c(NA, 4, NA, NA),
c4 = c(2, 3, 7, Inf)
)
# find only character "2"s
row_count(dat, count = "2", allow_coercion = FALSE)
# find only factor level "2"s
row_count(dat, count = factor("2"), allow_coercion = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.