Returned as tibble, with percentages
tab(vctr, .drop = FALSE)a vector to tabulate
Handling of factor levels that don't appear in the data, passed
on to group_by().
For count(): if FALSE will include counts for empty groups (i.e. for
levels of factors that don't exist in the data).
For
add_count(): deprecated since it
can't actually affect the output.
tibble with columns value, n, perc
vctr <- sample(LETTERS[1:5], 1000, TRUE)
tab(vctr)
#> # A tibble: 5 × 3
#> value n perc
#> <chr> <formttbl> <formttbl>
#> 1 A 210 21.0%
#> 2 D 209 20.9%
#> 3 B 200 20.0%
#> 4 E 191 19.1%
#> 5 C 190 19.0%
# Includes NAs count
vctr <- sample(c(LETTERS[1:5], NA), 1000, TRUE)
tab(vctr)
#> # A tibble: 6 × 3
#> value n perc
#> <chr> <formttbl> <formttbl>
#> 1 B 179 17.9%
#> 2 A 176 17.6%
#> 3 C 171 17.1%
#> 4 NA 169 16.9%
#> 5 E 155 15.5%
#> 6 D 150 15.0%
# For factors, by default keeps levels not present in data
vctr <- factor(
x = sample(c(LETTERS[1:5], NA), 1000, TRUE),
levels = c(LETTERS[1:5], "Levels", "not present", "in data")
)
tab(vctr)
#> # A tibble: 9 × 3
#> value n perc
#> <fct> <formttbl> <formttbl>
#> 1 NA 183 18.3%
#> 2 C 174 17.4%
#> 3 A 166 16.6%
#> 4 D 165 16.5%
#> 5 E 161 16.1%
#> 6 B 151 15.1%
#> 7 Levels 0 0.0%
#> 8 not present 0 0.0%
#> 9 in data 0 0.0%