factor()

factor()

The factor() function will convert a quantitative variable into a factor (a categorical variable). This is often needed when categorical variables are dummy coded as numeric values so R treats them as a quantitative variable. For example, when a variable such as "Gender" is coded as "1" for males and "2" for females. 

Another scenario, as seen in the example below, might be when you have used the ntile() function to create equal-sized groups out of a quantitative variable and it automatically assigns each group a number, leaving R to assume it is a quantitative variable. Therefore, you can use factor() to tell R that it is a categorical variable (you can also relabel the values as well).

(See as.numeric() for converting categorical variables into quantitative variables)

Idea
Example:

# creates two equal sized groups
Fingers$Height_2 <- ntile(Fingers$Height2)
# designates the new variable as a factor and labels the group numbers
Fingers$Height_2_factor <- factor(Fingers$Height_2, levels = c(1, 2), labels = c("short",  "tall"))
# check the data frame
head(select(FingersHeightHeight_2, Height_2_factor))

Example output:
output of the head function showing 6 rows of Height, Height_2, and Height_2_factor