Search code examples
rrecode

recode() in R does not alter the column


I have the following problem: I would like to recode a column in my dataset but the following code does not change anything in the data set and it also does not issue an error.

ESS.subset$agea.rc <- recode(ESS.subset$agea,'"<21"=1;"22-30"=2;"31-40"=3;"41-50"=4;"51-60"=5;"61-70"=6;">71"=7')

I also tried using mutate() from the dplyr package but to no avail.

I really have no clue why such a basic command does not work in my case and would really appreciate your help.

Thank you!


Solution

  • You need to give it a named vector like this:

    
    ESS.subset <- data.frame(
        agea.rc = c( "<21","22-30","31-40","41-50","51-60","61-70",">71")
    )
    
    ESS.subset$agea.rc <- 
        recode(ESS.subset$agea,"<21"=1,"22-30"=2,"31-40"=3,"41-50"=4,"51-60"=5,"61-70"=6,">71"=7)