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 213 21.3%
#> 2 D 207 20.7%
#> 3 B 202 20.2%
#> 4 C 189 18.9%
#> 5 E 189 18.9%
# 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 180 18.0%
#> 2 A 171 17.1%
#> 3 NA 170 17.0%
#> 4 C 169 16.9%
#> 5 E 160 16.0%
#> 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 181 18.1%
#> 2 C 171 17.1%
#> 3 A 169 16.9%
#> 4 D 165 16.5%
#> 5 E 162 16.2%
#> 6 B 152 15.2%
#> 7 Levels 0 0.0%
#> 8 not present 0 0.0%
#> 9 in data 0 0.0%