Back to Practice
#0162

Count Factor Levels in R

EasyR15 min10 XP

Problem

Given a factor vector of segments, return counts per segment.

Why This Matters

Factors are central to R data analysis, especially for categories, models, and plots.

Examples

Example 1
Inputfree, pro, free, team
Outputfree = 2, pro = 1, team = 1

table counts how many times each factor level appears.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Keep the code readable and deterministic.
CodeR
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputfree, pro, free, team
Expectedfree = 2, pro = 1, team = 1

table counts how many times each factor level appears.

Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input