as the comments above have pointed out, your column "out" is a factor, try str(dataset$out). In factors, you have levels which are predefined values, and they are used for many purposes.
For example:
x = LETTERS[1:5]
x = x[-1]
table(x[-1])
x = factor(LETTERS[1:5])
levels(x)
x = x[-1]
levels(x)
table(x[-1])
In the example above, even if you remove 'A' from the vector x
, because the levels are predefined, it shows you it is missing 'A' when you table it.
So you cannot replace an element in a factor column, with something that doesn't exist in the levels.
x = factor(LETTERS[1:5])
# ok
x[1] = "E"
# not ok
x[1] = "F"
So for your data, do:
dataset <- data.frame(id=1:5,out=c('1',LETTERS[2:5]))
dataset
id out
1 1 1
2 2 B
3 3 C
4 4 D
5 5 E
dataset$out <- with(dataset,replace(as.character(out),out=='1','A'))
dataset
id out
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
dataset$out <- factor(dataset$out)
In the above, I am converting them to a character first, then replacing those that are '1' with 'A'. You can convert them back to factor afterwards, if need be.