Hello friends,
I have taken the iris dataset as an example as the target variable is a categorical variable with 3 categories
Setosa
2)Versicolor
Virginica
Do we have to assign a number like 1 to Setosa 2 to Versicolor and 3 to Virginica and then convert it to a factor variable
OR
just convert it to a factor variable without assigning and number to each category....
Thanks,
Amod Shirke
No, you do not need to assign numbers to categorical variables in order to convert them to factors (though note that, for the example you give, species in the Iris dataset, your variable is already a factor by default—so I'm converting it to a character and then back to factor in the reprex below).
Created on 2018-09-09 by the reprex package (v0.2.0.9000).
Above I've used the base R function as.factor(), which doesn't require that you specify your factor levels. It's often a good idea to do so, though. See the forcats package docs, for example, for more detail on working with factors.