Search code examples
rif-statementr-factor

Replacing the values of a categorical variable using if-else condition or any other approach


This is my dataset “Survey” which contains “Codes” as a categorical variable.

ID<- seq(1:50)
Codes<- sample(c("Sat1", "Sat2", "A1", "B2", "C2", "D5", "T4"), 50, replace= TRUE)
Survey<- data.frame(Codes)

The levels in Codes are shown as follows:

> levels(Survey$Codes)
[1] "A1"   "B2"   "C2"   "D5"   "Sat1" "Sat2" "T4"  

I want to replace all the values in Codes column except “Sat1” and “Sat2” with a new value “Sat3”. In other words, I expect my levels in Codes to be:

> levels(Survey$Codes)
[1] “Sat1” “Sat2” “Sat3”

I know the first part of code to this problem i.e.

levels(Survey$Codes)<- c(levels(Survey$Codes), “Sat3”)

but I am stuck how to proceed with the rest of the code i.e. how to replace all the values of Codes (except “Sat1” and “Sat2”) with “Sat3”. I am thinking to use an if-else statement here but not sure how to frame the codes for it. Could you please help me with this? Thanks


Solution

  • How about

    levels(Survey$Codes)[!grepl("Sat",levels(Survey$Codes))]<-"Sat3"
    

    grepl checks if a certain string occurs in your levels. Output:

    > levels(Survey$Codes)
    [1] "Sat3" "Sat1" "Sat2"
    
    > Survey$Codes
     [1] Sat3 Sat1 Sat3 Sat2 Sat3 Sat3 Sat3 Sat3 Sat3 Sat3 Sat3 Sat3 Sat1 Sat3 Sat2 Sat3 Sat1 Sat3 Sat1 Sat2 Sat3 Sat3 Sat3 Sat2 Sat3
    [26] Sat1 Sat3 Sat3 Sat3 Sat3 Sat3 Sat3 Sat3 Sat2 Sat3 Sat3 Sat1 Sat3 Sat3 Sat3 Sat3 Sat2 Sat3 Sat3 Sat3 Sat2 Sat3 Sat3 Sat3 Sat3
    Levels: Sat3 Sat1 Sat2