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?