purrr Exercises in R: 50 Real Practice Problems
Fifty practice problems on purrr: map family, walk, reduce, safely/possibly, predicate variants, and many-models. Hidden solutions.
Section 1. map family basics (10 problems)
Exercise 1.1: map a function
Difficulty: Beginner. Square each of 1:5 returning a list.
Show solution
Exercise 1.2: map_dbl
Difficulty: Beginner. Same but return numeric.
Show solution
Exercise 1.3: map_chr
Difficulty: Beginner. Convert numbers to strings.
Show solution
Exercise 1.4: map_int
Difficulty: Beginner. Length of each list element.
Show solution
Exercise 1.5: map_lgl
Difficulty: Intermediate. Are means positive?
Show solution
Exercise 1.6: map_dfr
Difficulty: Intermediate. Build a tibble from a list of vectors.
Show solution
Exercise 1.7: map_dfc
Difficulty: Advanced. Combine columnwise.
Show solution
Exercise 1.8: map with named list
Difficulty: Intermediate. Apply mean per element.
Show solution
Exercise 1.9: map a function with extra args
Difficulty: Intermediate. Pass na.rm = TRUE.
Show solution
Exercise 1.10: map with anonymous function
Difficulty: Intermediate. Use \() shorthand (R 4.1+).
Show solution
Section 2. map2 and pmap (8 problems)
Exercise 2.1: map2_dbl
Difficulty: Intermediate. Element-wise x^y.
Show solution
Exercise 2.2: map2_chr
Difficulty: Intermediate. Combine two vectors into formatted strings.
Show solution
Exercise 2.3: pmap with three vectors
Difficulty: Advanced. Compute x*y + z.
Show solution
Exercise 2.4: pmap with a tibble
Difficulty: Advanced. Treat each row as args.
Show solution
Exercise 2.5: imap (with index)
Difficulty: Intermediate. map with the index/name.
Show solution
Exercise 2.6: walk for side effects
Difficulty: Intermediate. Print each element.
Show solution
Exercise 2.7: walk2
Difficulty: Intermediate. Save plots per group.
Show solution
Exercise 2.8: pwalk
Difficulty: Advanced. Iterate over multiple parallel inputs for side effects.
Show solution
Section 3. reduce and accumulate (6 problems)
Exercise 3.1: Sum with reduce
Difficulty: Beginner.
Show solution
Exercise 3.2: Reduce with accumulator
Difficulty: Intermediate. Running max.
Show solution
Exercise 3.3: Reduce list of data frames
Difficulty: Advanced. Inner-join three.
Show solution
Exercise 3.4: Reduce with init
Difficulty: Intermediate. Sum starting from 100.
Show solution
Exercise 3.5: Reduce right-to-left
Difficulty: Advanced.
Show solution
Exercise 3.6: Build cumulative product
Difficulty: Intermediate.
Show solution
Section 4. Predicates and filtering (8 problems)
Exercise 4.1: keep
Difficulty: Beginner. Keep list elements with mean > 5.
Show solution
Exercise 4.2: discard
Difficulty: Beginner. Drop NAs from a list.
Show solution
Exercise 4.3: detect (first match)
Difficulty: Intermediate. First element with length > 2.
Show solution
Exercise 4.4: detect_index
Difficulty: Intermediate. Position of first match.
Show solution
Exercise 4.5: every
Difficulty: Intermediate. Are all elements positive?
Show solution
Exercise 4.6: some
Difficulty: Intermediate. Any element negative?
Show solution
Exercise 4.7: keep_at
Difficulty: Advanced. Keep specific named elements of a list.
Show solution
Exercise 4.8: compact
Difficulty: Intermediate. Drop NULL/empty elements.
Show solution
Section 5. Errors and safety (6 problems)
Exercise 5.1: safely
Difficulty: Intermediate. Wrap log so it never errors.
Show solution
Exercise 5.2: Extract results from safely
Difficulty: Intermediate. Pluck results.
Show solution
Exercise 5.3: possibly
Difficulty: Intermediate. Replace failures with default.
Show solution
Exercise 5.4: quietly
Difficulty: Advanced. Capture warnings/messages.
Show solution
Exercise 5.5: Handle a batch of file reads
Difficulty: Advanced. Read CSVs, capture errors.
Show solution
Exercise 5.6: rate_backoff for retries
Difficulty: Advanced. Retry up to 3 times.
Show solution
Section 6. Many models and pipelines (4 problems)
Exercise 6.1: lm per group
Difficulty: Advanced. Fit lm per Species.
Show solution
Exercise 6.2: Tidy results
Difficulty: Advanced. Get coefficient tibbles per group.
Show solution
Exercise 6.3: Per-group R-squared
Difficulty: Advanced.
Show solution
Exercise 6.4: Predict with each model
Difficulty: Advanced. Add fitted values back.
Show solution
Section 7. Composition and currying (8 problems)
Exercise 7.1: compose
Difficulty: Advanced. Compose abs(log(x)).
Show solution
Exercise 7.2: partial application
Difficulty: Advanced. Make a paste with fixed sep.
Show solution
Exercise 7.3: Negate
Difficulty: Intermediate. Invert a predicate.
Show solution
Exercise 7.4: list_modify
Difficulty: Advanced. Update list elements.
Show solution
Exercise 7.5: list_flatten
Difficulty: Intermediate.
Show solution
Exercise 7.6: list_rbind
Difficulty: Intermediate. Modern alternative to map_dfr.
Show solution
Exercise 7.7: pluck
Difficulty: Intermediate. Deep extraction from nested list.
Show solution
Exercise 7.8: as_mapper string shortcut
Difficulty: Advanced. Use a string to access by name.
Show solution
What to do next
- R-Functional-Programming-Exercises (coming), base R FP idioms.
- tidyverse-Exercises (shipped), purrr inside larger pipelines.
- Apply-Family-Exercises (coming), purrr alternatives.