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



    • Related Articles

    • factor (in R)

      Factor (in R) is a type of R object that is a categorical variable.
    • as.numeric()

      The as.numeric() function will convert a factor or character value into a numeric value. For instance, if you need to change a factor such as `Gender` into numeric values, the as.numeric() function will assign each group a number and convert it from ...
    • ntile()

      The ntile() function can be used to create equal sized groups (n-tiles) out of a quantitative variable. It can be modified to make any number of groups. For instance, if you take a quantitative variable such as Height from the Fingers data frame, you ...
    • Arguments (in R Functions)

      Arguments (in R Functions) R function arguments are the inputs provided to a function that determine how it runs and what results it produces. Arguments are placed inside the parentheses of a function call and may represent data, options, or control ...
    • Variables

      A variable is a measurable characteristic or attribute that can take on different values across cases (i.e., observational units such as people, companies, or time points). Types of Variables Variables can be classified by the type of values they ...