Skip to contents

Reduces x integer codes with appropriate decorations. Result is like as.integer(as.numeric(x)) + offset but has a guide attribute: a list of integers whose names are the original levels of x. A wrapper for as.integer(classified(x).

Usage

# Default S3 method
codify(
  x,
  offset = 0L,
  ...,
  persistence = getOption("yamlet_persistence", TRUE)
)

Arguments

x

anything for which there is a 'classified' method; see classified

offset

an integer value to add to intermediate result

...

passed to classified; for relevance, see classified.default

persistence

whether to return 'dvec' (is.integer(): TRUE) or just integer.

Value

integer (possibly of class dvec)

Examples

library(magrittr)

# convert to decorated integer
c('knife','fork','spoon') %>% codify
#> [1] 2 1 3
#> attr(,"guide")
#> attr(,"guide")$fork
#> [1] 1
#> 
#> attr(,"guide")$knife
#> [1] 2
#> 
#> attr(,"guide")$spoon
#> [1] 3
#> 

# implement offset
c('knife','fork','spoon') %>% codify(-1)
#> [1] 1 0 2
#> attr(,"guide")
#> attr(,"guide")$fork
#> [1] 0
#> 
#> attr(,"guide")$knife
#> [1] 1
#> 
#> attr(,"guide")$spoon
#> [1] 2
#> 

# globally defeat the 'persistence' paradigm
options(yamlet_persistence = FALSE)
c('knife','fork','spoon') %>% 
  codify %>% 
  class # integer
#> [1] "integer"
  
# remove option to restore default persistence paradigm
options(yamlet_persistence = NULL)
c('knife','fork','spoon') %>% 
  codify %>%
  class # dvec
#> [1] "dvec"
  
# locally defeat persistence paradigm
c('knife','fork','spoon') %>% 
  codify(persistence = FALSE) %>% 
  class # integer
#> [1] "integer"
  
# pass levels
c('knife','fork','spoon') %>%
  codify(levels = c('spoon', 'knife', 'fork'))
#> [1] 2 3 1
#> attr(,"guide")
#> attr(,"guide")$spoon
#> [1] 1
#> 
#> attr(,"guide")$knife
#> [1] 2
#> 
#> attr(,"guide")$fork
#> [1] 3
#>