Factor category and barplot help

Split from How to give bar labels using barplot() function in Rstudio


I have another doubt to trouble you...sorry for that...:grinning:
I have taken the iris dataset as an example as the target variable is a categorical variable with 3 categories

  1. Setosa
    2)Versicolor
  2. 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

Hi Amod,

It sounds like this question is unrelated to your original post. To maximize your ability to get help and the utility of extant threads for people who come across them, it's best to open a new topic in such situations.

Please see the "Keep it tidy" section of the site guidelines.

Thanks

3 Likes

When you convert a variable to a factor, it will automatically assign an integer to each level.

Applying the function as.factor applies factor levels in alphanumeric order.

Check out the factor chapter of R4DS,

It actually has a few nice examples of converting variables into factors, plotting them, and then refactoring their levels.