'How can I combine 2 levels in a variable in R [duplicate]

I have a variable that looks like this:

> levels(Cambodia$`Income changed after 2020`)
[1] "Yes, I lost a total of income"        "Yes, I lost a partial of income"      "No, I earn the same amount of income"
[4] "Yes, I earn more income" 

I want to combine "Yes, I earn more income" and "No, I earn the same amount of income" to form a new level in the variable, so that it would look like:

> levels(Cambodia$`Income changed after 2020`)
[1] "Yes, I lost a total of income"        "Yes, I lost a partial of income"      "No income loss" 

I tried doing this but it did not work:

library(forcats)
Cambodia %>% fct_collapse(Cambodia$`Income changed after 2020` == c("Yes, I earn more income","No, I earn the same amount of income"))
r


Solution 1:[1]

Figured it out:

levels(Cambodia$`Income changed after 2020`)[levels(Cambodia$`Income changed after 2020`)%in%c("Yes, I earn more income","No, I earn the same amount of income")] <- "no income change"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Natasha H