Back to Practice
#0090

Count Factor Levels

EasyR12 min10 XP

Problem

Given status <- factor(c("new", "paid", "new", "cancelled")), count how many times each level appears.

Why This Matters

Factors represent categorical data in R. Counting levels is one of the first checks before modeling or plotting.

Examples

Example 1
Inputnew, paid, new, cancelled
Outputcancelled 1, new 2, paid 1

table counts each category.

Constraints

  • Use table.
  • Keep factor categories.
  • Understand output names are levels.
CodeR
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Status counts
Inputfactor status vector
Expectednew count is 2

new appears twice.

Hidden Test Categories
unused factor levelscharacter vector before factorNA category handling