This answer may very well be obvious (I hope it is), but I kept only finding convoluted solutions. What I'd like to do is conditionally revalue a factor based on the levels of another factor.
Here's an example using the mtcars dataset:
data(mtcars)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$am <- as.factor(mtcars$am)
table(mtcars$gear, mtcars$am) # examining the levels
levels(mtcars$gear)
# [1] "3" "4" "5"
levels(mtcars$am)
"0" "1"
Now among those cars with a gear level of "5", how can I assign a new "gear" level of "6" to those with an "am" level of "1", while retaining the factor levels "3","4","5" for "gear"? This is a much simpler example, but given the complexity of my dataset I'd prefer to keep the vectors as factors (and not transform to numeric and back, for example).