Search code examples
rr-factor

Specify a "too large" level when converting numeric to factor in R


When converting a numeric value to a factor, can I specify the level values at which the conversion occurs? Is it possible to designate a special level as "too large" or say "5+" without defining a(n inline) function?

For example:

c(1,2,20,3,10)

would be converted to:

factor(c("1","2","many","3","many"))

Solution

  • Yes, you can use cut, e.g.,

    v = c(1,2,20,3,10)
    cut(v, c(0:5, Inf), labels = c(1:5, "many"), right = T, include.lowest = T)
    

    yields

     [1] 1    2    many 3    many
     Levels: 1 2 3 4 5 many
    

    cut has a number of additional parameters to control how values get placed in the bins, so read the help there.