forcats Exercises in R: 40 Real Practice Problems
Forty practice problems on forcats: ordering, reordering, recoding, lumping, releveling, dropping unused levels. Hidden solutions, runnable code.
Section 1. Creating and inspecting (6 problems)
Exercise 1.1: Create a factor
Difficulty: Beginner.
Show solution
Exercise 1.2: Inspect levels
Difficulty: Beginner.
Show solution
Exercise 1.3: Count by level
Difficulty: Beginner.
Show solution
Exercise 1.4: Unique levels (factor)
Difficulty: Beginner.
Show solution
Exercise 1.5: Identify unused levels
Difficulty: Intermediate.
Show solution
Exercise 1.6: Check factor type
Difficulty: Beginner.
Show solution
Section 2. Reordering (10 problems)
Exercise 2.1: fct_relevel, explicit order
Difficulty: Beginner.
Show solution
Exercise 2.2: fct_relevel, move to first
Difficulty: Intermediate. Move "virginica" first.
Show solution
Exercise 2.3: fct_relevel, move to last
Difficulty: Intermediate.
Show solution
Exercise 2.4: fct_infreq, by frequency
Difficulty: Intermediate. Reorder diamonds$cut by descending frequency.
Show solution
Exercise 2.5: fct_inorder, by appearance
Difficulty: Intermediate.
Show solution
Exercise 2.6: fct_reorder, by another variable
Difficulty: Intermediate. Reorder by mean.
Show solution
Exercise 2.7: fct_reorder2 for line plots
Difficulty: Advanced. Reorder by last value of y.
Show solution
Exercise 2.8: fct_rev
Difficulty: Beginner. Reverse level order.
Show solution
Exercise 2.9: fct_shift
Difficulty: Intermediate. Shift levels cyclically by 1.
Show solution
Exercise 2.10: fct_shuffle
Difficulty: Intermediate. Random level order.
Show solution
Section 3. Recoding and grouping (8 problems)
Exercise 3.1: fct_recode
Difficulty: Intermediate. Rename "setosa" -> "S".
Show solution
Exercise 3.2: fct_collapse, multi to one
Difficulty: Intermediate. Lump levels.
Show solution
Exercise 3.3: fct_lump_n, keep top N
Difficulty: Intermediate. Keep top 3 cut categories, lump rest into "Other".
Show solution
Exercise 3.4: fct_lump_prop, by proportion
Difficulty: Advanced.
Show solution
Exercise 3.5: fct_lump_min, by min count
Difficulty: Intermediate.
Show solution
Exercise 3.6: fct_other, keep specific
Difficulty: Intermediate. Keep "Ideal", others lump.
Show solution
Exercise 3.7: fct_drop, remove unused
Difficulty: Intermediate.
Show solution
Exercise 3.8: fct_expand, add new levels
Difficulty: Advanced.
Show solution
Section 4. Conversion and applying (6 problems)
Exercise 4.1: as_factor (preserves order of appearance)
Difficulty: Intermediate.
Show solution
Exercise 4.2: factor() vs as.factor() vs as_factor()
Difficulty: Advanced. Compare ordering.
Show solution
Exercise 4.3: To character
Difficulty: Beginner.
Show solution
Exercise 4.4: Numeric from factor
Difficulty: Advanced. Pitfall: as.numeric returns level codes.
Show solution
Exercise 4.5: Reorder by aggregate function
Difficulty: Advanced.
Show solution
Exercise 4.6: Apply ordering to a ggplot
Difficulty: Intermediate.
Show solution
Section 5. Real workflows (10 problems)
Exercise 5.1: Bar chart ordered by frequency
Difficulty: Intermediate.
Show solution
Exercise 5.2: Bar chart with rare lumped
Difficulty: Advanced.
Show solution
Exercise 5.3: Boxplot ordered by median
Difficulty: Advanced.
Show solution
Exercise 5.4: Combine recode + lump
Difficulty: Advanced.
Show solution
Exercise 5.5: Drop unused after filter
Difficulty: Intermediate.
Show solution
Exercise 5.6: Reorder by sum with .fun
Difficulty: Advanced.
Show solution
Exercise 5.7: Manual interleave of two factors
Difficulty: Advanced.
Show solution
Exercise 5.8: Map to ordinal
Difficulty: Intermediate.
Show solution
Exercise 5.9: Visual reorder for ggplot dotplot
Difficulty: Advanced.
Show solution
Exercise 5.10: Stable lump with explicit "Other" position
Difficulty: Advanced.
Show solution
What to do next
- dplyr-Exercises (shipped), factors in larger wrangling pipelines.
- ggplot2-Exercises (shipped), factor ordering and viz.