Skip to content

Replaces factor levels with arbitrary numeric identifiers. Neither the values nor the order of the levels are preserved.

Usage

fct_anon(f, prefix = "")

Arguments

f

A factor.

prefix

A character prefix to insert in front of the random labels.

Examples

gss_cat$relig %>% fct_count()
#> # A tibble: 16 × 2
#>    f                           n
#>    <fct>                   <int>
#>  1 No answer                  93
#>  2 Don't know                 15
#>  3 Inter-nondenominational   109
#>  4 Native american            23
#>  5 Christian                 689
#>  6 Orthodox-christian         95
#>  7 Moslem/islam              104
#>  8 Other eastern              32
#>  9 Hinduism                   71
#> 10 Buddhism                  147
#> 11 Other                     224
#> 12 None                     3523
#> 13 Jewish                    388
#> 14 Catholic                 5124
#> 15 Protestant              10846
#> 16 Not applicable              0
gss_cat$relig %>%
  fct_anon() %>%
  fct_count()
#> # A tibble: 16 × 2
#>    f         n
#>    <fct> <int>
#>  1 01      224
#>  2 02      147
#>  3 03       23
#>  4 04      104
#>  5 05      689
#>  6 06       93
#>  7 07       95
#>  8 08       15
#>  9 09       71
#> 10 10       32
#> 11 11     5124
#> 12 12        0
#> 13 13    10846
#> 14 14      388
#> 15 15     3523
#> 16 16      109
gss_cat$relig %>%
  fct_anon("X") %>%
  fct_count()
#> # A tibble: 16 × 2
#>    f         n
#>    <fct> <int>
#>  1 X01   10846
#>  2 X02      71
#>  3 X03     104
#>  4 X04       0
#>  5 X05      95
#>  6 X06     388
#>  7 X07     689
#>  8 X08     109
#>  9 X09      23
#> 10 X10    5124
#> 11 X11      93
#> 12 X12     224
#> 13 X13      32
#> 14 X14    3523
#> 15 X15      15
#> 16 X16     147