lundi 14 juin 2021

How to use if_else and mutate to substitute the value in charactor vector as condition?

I want to generate one column in data with previous value if the condition in if_else are/aren`t consistent with, the value will be the same as the original column.

Here is the code:

iristest <- iris
iristest %>% mutate(Species=if_else(Species=='setosa','Setosa',Species))

Here is the error:

#Error: Problem with `mutate()` column `Species`.
#i `Species = if_else(Species == "setosa", "Setosa", Species)`.
#x `false` must be a character vector, not a `factor` object.

How do I solve the problem? I know it can be solved with a char function such as stringr, but I hope and think it could be solved in a mutate way.

Aucun commentaire:

Enregistrer un commentaire