When to use factor() function in R?

830    Asked by varshaChauhan in Data Science , Asked on Nov 29, 2019
Answered by varsha Chauhan

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 .



Your Answer

Interviews

Parent Categories