Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

factors-r

Factors

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Factors: the syntax, patterns, and safety habits you need before advancing in R.

Vectors are atomic—everything in R is vectorized; factors encode categorical variables for modeling.

You will apply Factors in contexts like: Research pipelines, Shiny dashboards, and statistical reporting.

Write R in the editor and click Run on server—the dev runner executes with Rscript; use print() or cat() and base R in playground snippets (tidyverse locally; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A factor stores categorical data with predefined levels—critical for statistical models and ggplot2 aesthetics (local). Base R uses factors in modeling formulas.

Creating factors

grades <- factor(c("B", "A", "C", "A"), levels = c("A", "B", "C"))
print(grades)
print(table(grades))

Why factors matter

  • Models treat categories correctly in lm(y ~ group)
  • Control ordering of bars in charts
  • Avoid treating codes as numeric accidentally

In tidyverse workflows, forcats (local) refines factor levels—playground uses base factor().

Important interview questions and answers

  1. Q: Factor vs character?
    A: Factors store levels with an integer backing; characters are plain strings without level metadata.
  2. Q: When convert to factor?
    A: Before modeling or when plot order should follow category order, not alphabet.

Self-check

  1. What function creates a factor?
  2. Why specify levels explicitly?

Tip: Set factor levels explicitly to control plot and table order—not alphabetical surprises.

Interview prep

Factor vs character?

Factors store level metadata for modeling and ordered legends; characters are plain strings.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Ordered factor?
  • as.factor when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump