Counts how often a specific value appears in each row across the selected variables. Useful for data quality checks (e.g., "How many items did a respondent answer with -9?") or for creating count-based indices.
Arguments
- data
Your survey data (a data frame or tibble).
- ...
The variables to check. Supports tidyselect.
- count
The value(s) to count: one value (
count = 5) or a set (count = c(4, 5)counts cells equal to 4 or 5, like SPSS'sCOUNT n = v1 TO v5 (4, 5)).NAcounts missing values (SPSS keywordMISSING). Missing codes of imported data (e.g. -9 read byread_spss()as a tagged NA) are counted when listed, as SPSS'sCOUNTcounts user-missing values.- na.rm
If
TRUE(default),NAvalues are ignored. IfFALSE, any row containing a missing value that is not counted returnsNA.
See also
row_sums() for row-wise sums, row_means() for row-wise means
Other scale:
efa(),
pomps(),
reliability(),
row_means(),
row_sums()
Examples
library(dplyr)
data(survey_data)
# How many items did each respondent answer with the highest value (5)?
survey_data <- survey_data %>%
mutate(n_top = row_count(., trust_government, trust_media,
trust_science, count = 5))
# Top-2 box (4 or 5) and number of missing answers per respondent
survey_data <- survey_data %>%
mutate(n_top2 = row_count(., trust_government, trust_media,
trust_science, count = c(4, 5)),
n_miss = row_count(., trust_government, trust_media,
trust_science, count = NA))
