What Does as.factor() Do in R?

as.factor(x) converts a vector into a factor, R's type for categorical data, with one level for each distinct value in sorted order. Internally a factor is an integer vector of codes plus a character vector of level names. as.factor takes no arguments other than the vector: it cannot set the level order, rename levels, or mark the factor as ordered. For any of those use factor(). If x is already a factor, as.factor returns it unchanged.

Example With Output

colors <- c("red", "blue", "green", "blue", "red") f <- as.factor(colors) f # [1] red blue green blue red # Levels: blue green red levels(f) # [1] "blue" "green" "red" as.integer(f) # [1] 3 1 2 1 3 table(f) # f # blue green red # 2 1 2 str(f) # Factor w/ 3 levels "blue","green",..: 3 1 2 1 3

Three things to read from this output. The levels are the distinct values sorted alphabetically, not in order of first appearance. Each element is stored as the integer position of its level, so "red" is 3. And table counts by level, which is the main reason to convert before summarizing.

For a numeric vector the levels are sorted numerically, so as.factor(c(10, 2, 33)) has levels 2, 10, 33. For a character vector holding numbers the sort is alphabetical, so as.factor(c("10", "2", "33")) has levels 10, 2, 33.

as.factor vs factor

as.factor(x)factor(x, levels, labels, ordered)
Level orderSorted values, alwaysAny order you give in levels
Rename levelsNoYes, with labels
Ordered factorNoYes, with ordered = TRUE
Drop or add levelsNoYes, by listing them in levels
Already a factorReturned unchangedRebuilt, dropping unused levels
SpeedFaster on large vectorsSlightly slower

Use as.factor when the default sorted levels are fine and you only need R to treat the column as categorical. Use factor when the order matters. That is the case for any variable with a natural order, and for any plot or model where you want a specific reference level.

size <- c("low", "high", "medium", "low") as.factor(size) # Levels: high low medium (alphabetical, wrong for this data) factor(size, levels = c("low", "medium", "high"), ordered = TRUE) # Levels: low < medium < high (ordered factor)

An ordered factor prints its levels with < and allows comparisons such as x < "high". Modeling functions also treat ordered factors differently, using polynomial contrasts instead of treatment contrasts.

as.factor on a Data Frame Column

The common case is a data frame column read in as character or integer that should be categorical. Assign the result back to the column.

df <- data.frame(id = 1:4, group = c("b", "a", "b", "c"), dose = c(1, 2, 1, 2)) df$group <- as.factor(df$group) # or several columns at once df[c("group", "dose")] <- lapply(df[c("group", "dose")], as.factor) str(df)

Since R 4.0, read.csv and data.frame keep strings as character by default, so this conversion is an explicit step. Models such as lm(y ~ group, data = df) depend on it. A numeric dose of 1 and 2 is fitted as a slope, while a factor dose is fitted as two groups.

The as.numeric Trap

as.numeric on a factor returns the integer codes, not the original numbers. This is the most common factor bug in R.

f <- as.factor(c(10, 20, 10, 30)) as.numeric(f) # [1] 1 2 1 3 (level positions, not the values) as.numeric(as.character(f)) # [1] 10 20 10 30 (correct) as.numeric(levels(f))[f] # [1] 10 20 10 30 (correct and faster on long vectors)

Convert through as.character or index the levels. The same applies when a column that should be numeric became a factor by accident because one cell held text such as "missing".

Renaming and Reordering Levels

levels(f) <- c(...) renames levels by position, so the new names must be given in the current level order. Renaming c("blue", "green", "red") to c("Green", "Red", "Blue") would silently mislabel every value. Two options are safer. factor(f, levels = ..., labels = ...) renames by matching. relevel(f, ref = "red") makes one level the first without changing the data.

Key Takeaways

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the best industry standard?
What is the OSI layer in networking?
What are the tips for solving mathematical problems in coding interviews?
Do FAANG Companies Ask LeetCode Hard Questions?
Yes, but rarely: most reported FAANG coding questions sit at medium difficulty, and hard questions appear more often at senior levels.
What is the AI app for interview questions?
What is a good technical interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.