How to convert a column to a factor in R

· 2 min read · Updated March 14, 2026 · beginner
r factors data-types tidyverse

Factors are R’s way of handling categorical data. Here is how to convert columns to factors.

Using factor()

The base R factor() function gives you full control:

# Convert a character vector to factor
df <- data.frame(
  gender = c("male", "female", "male", "female")
)

df$gender <- factor(df$gender)
levels(df$gender)
# [1] "female" "male"

Specify custom levels and order

Control the order of levels explicitly:

# Define level order
df$education <- factor(
  c("high school", "bachelors", "masters", "phd"),
  levels = c("high school", "bachelors", "masters", "phd")
)

# Check the levels
levels(df$education)
# [1] "high school" "bachelors"  "masters"    "phd"

Using as.factor()

A shortcut that works similarly to factor():

df$status <- as.factor(c("active", "inactive", "active"))
# [1] active   inactive active  
# Levels: active inactive

Ordered factors

For ordinal data, create an ordered factor:

df$satisfaction <- factor(
  c("low", "high", "medium", "low", "high"),
  levels = c("low", "medium", "high"),
  ordered = TRUE
)

# Now you can compare
df$satisfaction[1] < df$satisfaction[2]
# [1] TRUE

With dplyr::mutate()

In the tidyverse, use mutate() with factor():

library(dplyr)

df <- df %>%
  mutate(
    gender = factor(gender, levels = c("female", "male")),
    satisfaction = factor(
      satisfaction,
      levels = c("low", "medium", "high"),
      ordered = TRUE
    )
  )

Convert numeric to factor

Sometimes you need to treat numbers as categories:

df$rating <- factor(
  c(1, 5, 3, 4, 2, 5),
  levels = 1:5,
  labels = c("poor", "fair", "good", "very good", "excellent")
)

levels(df$rating)
# [1] "poor"        "fair"        "good"        "very good"   "excellent"

See Also

  • factor — Full reference for the factor data type
  • data.frame — How data frames work in R
  • tibble — The tidyverse tibble alternative