readr Exercises in R: 30 Real Practice Problems

Thirty practice problems on readr: reading and writing CSVs, custom NAs, column types, locales, parsing helpers. Hidden solutions, runnable code.

RRun this once before any exercise
library(readr) library(dplyr) library(tibble) library(purrr)

  

Section 1. Reading basics (8 problems)

Exercise 1.1: read_csv

Difficulty: Beginner. Write and re-read a small CSV.

Show solution
RInteractive R
write_csv(mtcars, "demo.csv") df <- read_csv("demo.csv") head(df)

  

Exercise 1.2: read_tsv

Difficulty: Beginner.

Show solution
RInteractive R
write_tsv(mtcars, "demo.tsv") read_tsv("demo.tsv")

  

Exercise 1.3: Read with custom NA strings

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", na = c("","NA","N/A","missing"))

  

Exercise 1.4: Skip lines

Difficulty: Intermediate. Skip first 2 rows.

Show solution
RInteractive R
read_csv("demo.csv", skip = 2)

  

Exercise 1.5: First N rows only

Difficulty: Beginner. Read 5 rows.

Show solution
RInteractive R
read_csv("demo.csv", n_max = 5)

  

Exercise 1.6: From a string

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("a,b\n1,2\n3,4")

  

Exercise 1.7: Read without column names

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", col_names = FALSE)

  

Exercise 1.8: Custom column names

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", col_names = c("v1","v2","v3"), skip = 1)

  

Section 2. Column types (6 problems)

Exercise 2.1: Force a column to character

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", col_types = cols(mpg = col_character()))

  

Exercise 2.2: Compact spec string

Difficulty: Intermediate. cidD = char/int/double.

Show solution
RInteractive R
read_csv("a,b,c\nx,1,2.5", col_types = "cid")

  

Exercise 2.3: Skip a column

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", col_types = cols(disp = col_skip()))

  

Exercise 2.4: Date column

Difficulty: Intermediate. Parse a date column.

Show solution
RInteractive R
csv <- "id,date\n1,2024-01-15" read_csv(csv, col_types = cols(date = col_date()))

  

Exercise 2.5: Custom date format

Difficulty: Advanced.

Show solution
RInteractive R
csv <- "id,date\n1,15/01/2024" read_csv(csv, col_types = cols(date = col_date(format = "%d/%m/%Y")))

  

Exercise 2.6: Spec problems

Difficulty: Advanced. Inspect parsing problems.

Show solution
RInteractive R
df <- read_csv("a\nfoo\n3", col_types = cols(a = col_integer())) problems(df)

  

Section 3. Parsers (8 problems)

Exercise 3.1: parse_number from currency

Difficulty: Intermediate.

Show solution
RInteractive R
parse_number("$1,234.50")

  

Exercise 3.2: parse_double

Difficulty: Beginner.

Show solution
RInteractive R
parse_double(c("1.5","2.7","NA"))

  

Exercise 3.3: parse_logical

Difficulty: Intermediate.

Show solution
RInteractive R
parse_logical(c("TRUE","FALSE","T","F","NA"))

  

Exercise 3.4: parse_date

Difficulty: Intermediate.

Show solution
RInteractive R
parse_date("01/15/2024", format = "%m/%d/%Y")

  

Exercise 3.5: parse_datetime

Difficulty: Intermediate.

Show solution
RInteractive R
parse_datetime("2024-01-15T14:30:00")

  

Exercise 3.6: parse_number with custom locale

Difficulty: Advanced. Parse "1.234,50" (European).

Show solution
RInteractive R
parse_number("1.234,50", locale = locale(decimal_mark = ",", grouping_mark = "."))

  

Exercise 3.7: parse_factor

Difficulty: Intermediate.

Show solution
RInteractive R
parse_factor(c("low","high","med"), levels = c("low","med","high"))

  

Exercise 3.8: parse_guess

Difficulty: Advanced.

Show solution
RInteractive R
parse_guess(c("1.5","2","TRUE","2024-01-15"))

  

Section 4. Writing (4 problems)

Exercise 4.1: write_csv

Difficulty: Beginner.

Show solution
RInteractive R
write_csv(mtcars, "out.csv")

  

Exercise 4.2: write_excel_csv

Difficulty: Intermediate. Excel-friendly UTF-8 BOM.

Show solution
RInteractive R
write_excel_csv(mtcars, "out_excel.csv")

  

Exercise 4.3: write_rds for round-trip

Difficulty: Intermediate.

Show solution
RInteractive R
write_rds(mtcars, "out.rds") read_rds("out.rds")

  

Exercise 4.4: Append rows

Difficulty: Advanced.

Show solution
RInteractive R
write_csv(mtcars[1:5,], "incr.csv") write_csv(mtcars[6:10,], "incr.csv", append = TRUE)

  

Section 5. Real-world workflows (4 problems)

Exercise 5.1: Read a messy file

Difficulty: Advanced. Skip lines, custom NA, custom delimiter.

Show solution
RInteractive R
read_delim("comment_line\na;b\n1;2\n3;NA", skip = 1, delim = ";", na = "NA")

  

Exercise 5.2: Read multiple files

Difficulty: Advanced. Use map_dfr.

Show solution
RInteractive R
files <- c("demo.csv","demo.csv") # demo df <- purrr::map_dfr(files, read_csv, .id = "source") df

  

Exercise 5.3: Read large file in chunks

Difficulty: Advanced.

Show solution
RInteractive R
read_csv_chunked("demo.csv", callback = DataFrameCallback$new(function(x, pos) head(x, 2)), chunk_size = 5)

  

Exercise 5.4: Read with progress

Difficulty: Intermediate.

Show solution
RInteractive R
read_csv("demo.csv", progress = TRUE)

  

What to do next

  • Data-Wrangling-Exercises (shipped), clean data after import.
  • EDA-Exercises (shipped), explore the imported data.