There is a dataset in which I have to create labels conditionally using the cut function; after that, only one of the labels shall be changed. Here is a similar code:
data.frame(x = c(12, 1, 25, 12, 65, 2, 6, 17)) %>%
mutate(rank = cut(x, breaks = c(0, 3, 12, 15, 20, 80),
labels = c("First", "Second", "Third", "Fourth", "Fifth")))
The output will be as follows:
However, when I want to change that rank relevant to the x value of 17 only to "Seventeen" and keep the rest as original using the following code, all other values in the rank column will change as well:
data.frame(x = c(12, 1, 25, 12, 65, 2, 6, 17)) %>%
mutate(rank = cut(x, breaks = c(0, 3, 12, 15, 20, 80),
labels = c("First", "Second", "Third", "Fourth", "Fifth"))) %>%
mutate(rank = ifelse(x == 17, "Seventeen",rank))
and the output will look like:
How can I prevent this happening?


We have to wrap
rankinas.characterto avoid class incompatibilities (factor vs. character):Until 'Seventeen' is added to the
rankcolumn, we have a vector rank of class factor. With adding of 'Seventeen' to this column or (vector) factor changes I think als called coerces to character, because character is the strongest!