Having issues with converting words to a numeric value.

I have been having issues with trying to convert words to numerical values. I cannot find for the life of me what is going wrong. My code looks like below:

 df1 <- df1 %>%
mutate(income_num = case_when(
IncomeGroup == "Low income" ~ 1,
IncomeGroup == "Lower middle income" ~ 2,
IncomeGroup == "Upper middle income" ~ 3,
"TRUE" ~ 4))

My hope is to give a numerical value to the different types of incomes but I keep on getting this error message:

Error: Problem with mutate() input income_num . x object 'IncomeGroup' not found i Input income_num is case_when(...) . Run rlang::last_error() to see where the error occurred.

Help will very much be appreciated.

Compare

suppressPackageStartupMessages({
  library(dplyr)
})

df1 <- data.frame(id = 1:4, IncomeGroup = c("Low income", "Lower middle income", "Upper middle income", "TRUE"))

df1 <- df1 %>%
  mutate(income_num = case_when(
    IncomeGroup == "Low income" ~ 1,
    IncomeGroup == "Lower middle income" ~ 2,
    IncomeGroup == "Upper middle income" ~ 3,
    TRUE ~ 4))

df1
#>   id         IncomeGroup income_num
#> 1  1          Low income          1
#> 2  2 Lower middle income          2
#> 3  3 Upper middle income          3
#> 4  4                TRUE          4

This topic was automatically closed 21 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.