rename a number in a column to a name

I want to rename all the #1=Northeast, 2=Midwest, 3=South and 4=West under variable REGION. What am I doing wrong?

Thanks!!
regionnew<-mutate(nhis,region1=
(case_when(REGION=1~"Northeast",

                             REGION=2~"Midwest",
                             
                             REGION=3~"South",
                                                              
                             REGION=4~"West")))

Use REGION == instead of a single =.

1 Like

Use == for a comparison.

regionnew<-mutate(nhis,region1=
                    case_when(REGION == 1 ~ "Northeast",
                               
                               REGION == 2 ~ "Midwest",
                               
                               REGION == 3 ~ "South",
                               
                               REGION == 4 ~ "West"))

If REGION is already characters, put quotes around the digits, like REGION == "1"

I got this when i tried.

Error in mutate():
! Problem while computing region1 = (...).
Caused by error in case_when():
! LHS of case 1 ("1") must be a logical vector, not
a character vector.
Run rlang::last_error() to see where the error occurred.

never mind got it!!! thanks so much!!!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.