w <- factor(c("a","b","c"))
w %in% c("b","c")
w %in% c(2,3) #same as previous, but with warning
#Suppose that a variable x has levels "a", "b1", "b2" and you
#want to classify levels "b1" and "b2" as just "b". You can use
#the \%in\% operator and do the following:
# levels(x)[levels(x) \%in\% c("b1","b2")] <- "b"
#Note that levels(x) <- list(b=c('b1','b2')) would be a better approach
Run the code above in your browser using DataCamp Workspace