Count entries in a factor

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   h   i   j   l   n   p   q   r   t   u   w   x   y   z 
#> 102   6  14  51 107  43  93   2   1 117  25  51  64  53  11 122 132   5   1 
fct_count(f)
#> # A tibble: 19 × 2
#>    f         n
#>    <fct> <int>
#>  1 a       102
#>  2 b         6
#>  3 c        14
#>  4 e        51
#>  5 f       107
#>  6 h        43
#>  7 i        93
#>  8 j         2
#>  9 l         1
#> 10 n       117
#> 11 p        25
#> 12 q        51
#> 13 r        64
#> 14 t        53
#> 15 u        11
#> 16 w       122
#> 17 x       132
#> 18 y         5
#> 19 z         1
fct_count(f, sort = TRUE)
#> # A tibble: 19 × 2
#>    f         n
#>    <fct> <int>
#>  1 x       132
#>  2 w       122
#>  3 n       117
#>  4 f       107
#>  5 a       102
#>  6 i        93
#>  7 r        64
#>  8 t        53
#>  9 e        51
#> 10 q        51
#> 11 h        43
#> 12 p        25
#> 13 c        14
#> 14 u        11
#> 15 b         6
#> 16 y         5
#> 17 j         2
#> 18 l         1
#> 19 z         1
fct_count(f, sort = TRUE, prop = TRUE)
#> # A tibble: 19 × 3
#>    f         n     p
#>    <fct> <int> <dbl>
#>  1 x       132 0.132
#>  2 w       122 0.122
#>  3 n       117 0.117
#>  4 f       107 0.107
#>  5 a       102 0.102
#>  6 i        93 0.093
#>  7 r        64 0.064
#>  8 t        53 0.053
#>  9 e        51 0.051
#> 10 q        51 0.051
#> 11 h        43 0.043
#> 12 p        25 0.025
#> 13 c        14 0.014
#> 14 u        11 0.011
#> 15 b         6 0.006
#> 16 y         5 0.005
#> 17 j         2 0.002
#> 18 l         1 0.001
#> 19 z         1 0.001