Skip to contents

Coerces classified to integer. 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. If you need a simple integer, consider coercing first to numeric.

Usage

# S3 method for class 'classified'
as.integer(
  x,
  offset = 0L,
  ...,
  persistence = getOption("yamlet_persistence", TRUE)
)

Arguments

x

classified, see classified

offset

an integer value to add to intermediate result

...

passed to desolve

persistence

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

Value

integer (possibly of class dvec)

Examples

library(magrittr)

# create factor with codelist attribute
classified(c('knife','fork','spoon'))
#> [1] knife fork  spoon
#> attr(,"codelist")
#> attr(,"codelist")[[1]]
#> [1] fork
#> 
#> attr(,"codelist")[[2]]
#> [1] knife
#> 
#> attr(,"codelist")[[3]]
#> [1] spoon
#> 
#> Levels: fork knife spoon

# give back a simple numeric
classified(c('knife','fork','spoon')) %>% as.numeric
#> [1] 2 1 3

# intentionally preserve levels as 'guide' attribute
classified(c('knife','fork','spoon')) %>% as.integer
#> [1] 2 1 3
#> attr(,"guide")
#> attr(,"guide")$fork
#> [1] 1
#> 
#> attr(,"guide")$knife
#> [1] 2
#> 
#> attr(,"guide")$spoon
#> [1] 3
#> 

# implement offset
classified(c('knife','fork','spoon')) %>% as.integer(-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') %>% 
  classified %>%
  as.integer %>% 
  class # integer
#> [1] "integer"
  
# remove option to restore default persistence paradigm
options(yamlet_persistence = NULL)
c('knife','fork','spoon') %>% 
  classified %>%
  as.integer %>% 
  class # dvec
#> [1] "dvec"
  
# locally defeat persistence paradigm
c('knife','fork','spoon') %>% 
  classified %>%
  as.integer(persistence = FALSE) %>% 
  class # integer
#> [1] "integer"