R for Data Science Exercises: 50 Practice Problems
Fifty practice problems mapped to R for Data Science topics: visualize, transform, tidy, import, strings, dates, iteration, models. Hidden solutions.
Section 1. Visualize (8 problems)
Exercise 1.1: Scatter plot
Difficulty: Beginner. Scatter of mpg dataset displ vs hwy.
Show solution
Exercise 1.2: Color by class
Difficulty: Beginner. Same scatter, colored by class.
Show solution
Exercise 1.3: Facet by drv
Difficulty: Intermediate. Same scatter, facet by drv.
Show solution
Exercise 1.4: Add smoother
Difficulty: Intermediate. Add a smoother (default) to the displ-vs-hwy scatter.
Show solution
Exercise 1.5: Bar chart of cut
Difficulty: Beginner. Bar chart of diamonds$cut counts.
Show solution
Exercise 1.6: Histogram with binwidth
Difficulty: Intermediate. Histogram of diamonds$carat with binwidth = 0.1.
Show solution
Exercise 1.7: Position dodge for grouped bars
Difficulty: Intermediate. Bar of diamonds cut with fill = clarity, dodged side by side.
Show solution
Exercise 1.8: coord_flip for horizontal bars
Difficulty: Intermediate. Make the diamonds$cut bar chart horizontal.
Show solution
Section 2. Transform (8 problems)
Exercise 2.1: Filter
Difficulty: Beginner. mtcars rows with mpg > 25.
Show solution
Exercise 2.2: Arrange
Difficulty: Beginner. Sort mtcars by hp descending.
Show solution
Exercise 2.3: Select with helpers
Difficulty: Intermediate. Select all iris columns starting with "Sepal".
Show solution
Exercise 2.4: Mutate
Difficulty: Beginner. Add kpl = mpg * 0.425 to mtcars.
Show solution
Exercise 2.5: Summarise per group
Difficulty: Intermediate. Mean and SD of mpg per cyl.
Show solution
Exercise 2.6: Count
Difficulty: Beginner. Count diamonds per cut.
Show solution
Exercise 2.7: case_when
Difficulty: Intermediate. Bin diamonds price into "budget", "mid", "premium".
Show solution
Exercise 2.8: Pipeline
Difficulty: Intermediate. Filter ideal-cut, group by clarity, summarise mean price, sort desc.
Show solution
Section 3. Tidy (8 problems)
Exercise 3.1: pivot_longer
Difficulty: Beginner. Pivot a wide quarterly table to long.
Show solution
Exercise 3.2: pivot_wider
Difficulty: Beginner. Pivot a long table back to wide.
Show solution
Exercise 3.3: separate
Difficulty: Intermediate. Split "Last, First" into two columns.
Show solution
Exercise 3.4: unite
Difficulty: Intermediate. Combine year, month, day to ISO date string.
Show solution
Exercise 3.5: drop_na
Difficulty: Beginner. Drop airquality rows where Ozone is NA.
Show solution
Exercise 3.6: fill
Difficulty: Intermediate. Carry forward NA in a region column.
Show solution
Exercise 3.7: complete
Difficulty: Intermediate. Add missing month combinations with sales = 0.
Show solution
Exercise 3.8: nest
Difficulty: Advanced. Nest iris by Species.
Show solution
Section 4. Import & wrangle (6 problems)
Exercise 4.1: Read a CSV
Difficulty: Beginner. Read a small CSV with read_csv.
Show solution
Exercise 4.2: Custom NA values
Difficulty: Intermediate. Read CSV treating "N/A" and "" as NA.
Show solution
Exercise 4.3: Force a column type
Difficulty: Intermediate. Read CSV forcing id to character.
Show solution
Exercise 4.4: Write CSV
Difficulty: Beginner. Write mtcars to CSV.
Show solution
Exercise 4.5: parse_number
Difficulty: Intermediate. Convert "$1,234.50" to 1234.5.
Show solution
Exercise 4.6: Read multiple files
Difficulty: Advanced. Read 3 CSVs into one tibble with map_dfr.
Show solution
Section 5. Strings & dates (6 problems)
Exercise 5.1: Detect substring
Difficulty: Beginner. Filter emails containing "gmail.com".
Show solution
Exercise 5.2: Replace by regex
Difficulty: Intermediate. Strip non-digits from "(415) 555-1234".
Show solution
Exercise 5.3: Pad with zeros
Difficulty: Beginner. Format ID 42 as "000042".
Show solution
Exercise 5.4: Parse mixed dates
Difficulty: Intermediate. Parse "01/15/2024" and "2024-02-20" together.
Show solution
Exercise 5.5: Extract month
Difficulty: Beginner. Get month name from a date.
Show solution
Exercise 5.6: Floor to month start
Difficulty: Intermediate. Snap dates to month-start.
Show solution
Section 6. Iteration & functions (8 problems)
Exercise 6.1: map_dbl
Difficulty: Beginner. Square each of 1:5.
Show solution
Exercise 6.2: map_dfr
Difficulty: Intermediate. For each n in 1:3, return a tibble with n and squared.
Show solution
Exercise 6.3: map2
Difficulty: Intermediate. Element-wise x^y for two vectors.
Show solution
Exercise 6.4: keep
Difficulty: Intermediate. Keep list elements with mean > 5.
Show solution
Exercise 6.5: Write a simple function
Difficulty: Beginner. Function returning x squared.
Show solution
Exercise 6.6: Default arguments
Difficulty: Intermediate. Function with default n = 10.
Show solution
Exercise 6.7: Anonymous function
Difficulty: Intermediate. R 4.1+ shorthand for an anonymous function.
Show solution
Exercise 6.8: safely
Difficulty: Advanced. Wrap a risky function so it never throws.
Show solution
Section 7. Models (6 problems)
Exercise 7.1: Simple linear model
Difficulty: Beginner. Fit mpg ~ wt.
Show solution
Exercise 7.2: Read model summary
Difficulty: Intermediate. R-squared and slope p-value from the model.
Show solution
Exercise 7.3: Tidy with broom
Difficulty: Intermediate. Use broom::tidy to extract coefficients.
Show solution
Exercise 7.4: Many models per group
Difficulty: Advanced. Fit lm per cyl, return slopes.
Show solution
Exercise 7.5: Predict
Difficulty: Intermediate. Predict mpg for wt = 3.
Show solution
Exercise 7.6: Train/test split + RMSE
Difficulty: Advanced. 70/30 split, train lm, compute test RMSE.
Show solution
What to do next
After 50 R4DS-aligned problems you have a solid working toolkit. Natural follow-ups:
- dplyr-Exercises, ggplot2-Exercises, tidyverse-Exercises, Data-Wrangling-Exercises (all shipped), depth on each topic.
- EDA-Exercises (shipped), applied data-exploration drills.
- Linear-Regression-Exercises (shipped), modeling beyond R4DS basics.