Write R code to create a factor called size with the values "Small", "Medium", "Large", "Medium", and specify the order from smallest to largest. Then print the levels of this factor.
Factors are used to represent categorical data. They store both the values and the possible levels, which makes them useful for grouping and statistical modeling.
gender <- factor(c("Male", "Female", "Female", "Male"))
levels(gender)
You can also specify the order of levels:
education <- factor(c("Bachelor", "Master", "PhD", "Bachelor"),
levels = c("Bachelor", "Master", "PhD"),
ordered = TRUE)