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 "Sex" 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)

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.
    • argument

      Arguments are the elements of an R function (e.g., in the function ```factor()```, you enter in the variable name, the levels, and the labels -- these things that go in the parentheses are the arguments).
    • as.numeric()

      The as.numeric() function will convert a factorial value into a numeric value. For instance, if you need to change a factor such as `Sex` into numeric values, the as.numeric() function will assign each group a number and convert it from being coded ...
    • 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 ...