When to use factor() function in R?
factor() function is used when the data mostly contains categorical variables.This specific function will become extremely useful when we begin to apply data analysis and machine learning techniques to our data. This idea is sometimes also known as creating dummy variables.
animal <- c('d','c','d','c','c')
id <- c(1,2,3,4,5)
We can pass the vector through the factor() function like so to get this information:
factor.ani <- factor(animal)
# Will show levels as well on RStudio or R Console
factor.ani
d c d c c
In R, there are two distinct types of categorical variables, an ordinary categorical variable and a nominal categorical variable .