recode
Idiom for ifelse-style recoding for multiple categories
You could convert your variable to a factor and change its levels by levels<- function. In one command it could be like: `levels<-`( factor(dat$product), list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12) ) In steps: brands <- factor(dat$product) levels(brands) <- list(Tylenol=1:3, Advil=4:6, Bayer=7:9, Generic=10:12)
Recode categorical variable to binary (0/1)
Use ifelse as in: dat$new1 <- ifelse(dat$Q1==3, 1, 0)
Replace a value in a data frame based on a conditional (`if`) statement
Easier to convert nm to characters and then make the change: junk$nm <- as.character(junk$nm) junk$nm[junk$nm == “B”] <- “b” EDIT: And if indeed you need to maintain nm as factors, add this in the end: junk$nm <- as.factor(junk$nm)