R using ifelse to change factor values

if-statement, r

Solution

You are better off changing the `levels`:

set.seed(1)
x <- factor(sample(letters[1:3],10,replace=T))
x
 [1] a b b c a c c b b a
Levels: a b c

levels(x)[which(levels(x)=="c")] <- "z"
x
 [1] a b b z a z z b b a
Levels: a b z

Problem

I have a column that's a factor variable. I need to change the value of all cells where the factor is a certain rarely occurring level. I'm using the following code but it doesn't seem to be working: ``` test2$timeFactor <- ifelse(test2$timeFactor == '94', '-1000', test2$timeFactor) ``` I've also tried: ``` test2$timeFactor <- factor(ifelse(test2$timeFactor == '94', '-1000', test2$timeFactor)) ``` but neither seems to work. Anything obvious I'm missing here?

Original source