rdplyrrecode

How to recode a range of values (numeric) into a factor variable?


I want to recode a variable that has values from 1 to 10 (numeric) into a factor variable of 5 values. I'm using the command dplyr::recode, but it is not working. I know how to do it with the cut function or case_when, but i want to know if there is a way to do it with the recode function.

I'm trying this code but is showing an error:

df <- df %>% 
  mutate(x_rec = as.factor(recode(x, '1:2' = '1',
                                  '3:4' = '2', '5:6'= '3',
                                  '7:8' = '4', '9:10' = '5')))

The output shows this:

Warning messages:
1: Problem while computing `x_rec = as.factor(...)`.
ℹ NAs introduced by coercion 
2: Problem while computing `x_rec = as.factor(...)`.
ℹ Unreplaced values treated as NA as `.x` is not compatible. 

I tried to use drop_na(), but is not working. I also tried to use the argument ".default = NA" and ".missing = NA" but it says that is an unused argument. If someone can help me it would be great. Thanks a lot.


Solution

  • recode() will only help you if your input is a factor variable already. Use cut() to convert numerical values into factor levels.

    set.seed(42)
    x <- sample(1:10, 10, replace = TRUE)
    
    cut(x,
        breaks = c(0, 2, 4, 6, 8, 10),
        labels = c("1", "2", "3", "4", "5"))
    #>  [1] 1 3 1 5 5 2 1 5 1 4
    #> Levels: 1 2 3 4 5
    

    Created on 2023-03-09 with reprex v2.0.2