Home > OS >  keep the original value when using ifelse in dplyr after using cut
keep the original value when using ifelse in dplyr after using cut

Time:08-08

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:

enter image description here

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:

enter image description here

How can I prevent this happening?

CodePudding user response:

We have to wrap rank in as.character to avoid class incompatibilities (factor vs. character):

Until 'Seventeen' is added to the rank column, 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!

library(dplyr)

df %>% 
  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",as.character(rank)))
   x      rank
1 12    Second
2  1     First
3 25     Fifth
4 12    Second
5 65     Fifth
6  2     First
7  6    Second
8 17 Seventeen
  • Related