Skip to content

Count entries in a factor

Usage

fct_count(f, sort = FALSE, prop = FALSE)

Arguments

f

A factor (or character vector).

sort

If TRUE, sort the result so that the most common values float to the top.

prop

If TRUE, compute the fraction of marginal table.

Value

A tibble with columns f, n and p, if prop is TRUE.

Examples

f <- factor(sample(letters)[rpois(1000, 10)])
table(f)
#> f
#>   a   b   c   e   f   g   h   i   k   l   m   o   p   q   r   s   t   u 
#>  20  95  62  10   1  28  29   5  24  39   1   1   4 117 107   2 110  65 
#>   v   w   x   y 
#>   3  61 102 114 
fct_count(f)
#> # A tibble: 22 × 2
#>    f         n
#>    <fct> <int>
#>  1 a        20
#>  2 b        95
#>  3 c        62
#>  4 e        10
#>  5 f         1
#>  6 g        28
#>  7 h        29
#>  8 i         5
#>  9 k        24
#> 10 l        39
#> # ℹ 12 more rows
fct_count(f, sort = TRUE)
#> # A tibble: 22 × 2
#>    f         n
#>    <fct> <int>
#>  1 q       117
#>  2 y       114
#>  3 t       110
#>  4 r       107
#>  5 x       102
#>  6 b        95
#>  7 u        65
#>  8 c        62
#>  9 w        61
#> 10 l        39
#> # ℹ 12 more rows
fct_count(f, sort = TRUE, prop = TRUE)
#> # A tibble: 22 × 3
#>    f         n     p
#>    <fct> <int> <dbl>
#>  1 q       117 0.117
#>  2 y       114 0.114
#>  3 t       110 0.11 
#>  4 r       107 0.107
#>  5 x       102 0.102
#>  6 b        95 0.095
#>  7 u        65 0.065
#>  8 c        62 0.062
#>  9 w        61 0.061
#> 10 l        39 0.039
#> # ℹ 12 more rows