R Data Import Exercises: 10 read_csv(), read_excel() Practice Problems

Practice importing data in R with 10 exercises: read_csv(), read_delim(), column types, missing values, skipping rows, and combining files. Each has an interactive solution.

Easy (1-4)

Exercise 1: Read a Simple CSV

library(readr) csv <- "product,price,qty,in_stock\nLaptop,999.99,50,TRUE\nMouse,24.99,200,TRUE\nKeyboard,74.50,0,FALSE" # Read and print column types


  
Click to reveal solution ```r
library(readr) csv <- "product,price,qty,in_stock\nLaptop,999.99,50,TRUE\nMouse,24.99,200,TRUE\nKeyboard,74.50,0,FALSE" df <- read_csv(csv, show_col_types = FALSE) print(df) cat("\nTypes:", sapply(df, class), "\n")

  

Exercise 2: Pipe-Delimited File

library(readr) data <- "name|dept|salary\nAlice|Engineering|95000\nBob|Marketing|82000\nCarol|Sales|68000" # Read this pipe-delimited data


  
Click to reveal solution ```r
library(readr) data <- "name|dept|salary\nAlice|Engineering|95000\nBob|Marketing|82000\nCarol|Sales|68000" read_delim(data, delim = "|", show_col_types = FALSE)

  

Exercise 3: Custom NA Strings

library(readr) csv <- "id,score,grade\n1,88,A\n2,N/A,B\n3,,-\n4,-999,C\n5,76," # Read with all missing representations: "", "N/A", "-", "-999"


  
Click to reveal solution ```r
library(readr) csv <- "id,score,grade\n1,88,A\n2,N/A,B\n3,,-\n4,-999,C\n5,76," df <- read_csv(csv, na = c("", "NA", "N/A", "-", "-999"), show_col_types = FALSE) print(df) cat("Total NAs:", sum(is.na(df)), "\n")

  

Exercise 4: Preserve Leading Zeros

library(readr) csv <- "name,zipcode,phone\nAlice,01234,5551234567\nBob,00501,5559876543" # Read so zipcodes keep leading zeros


  
Click to reveal solution ```r
library(readr) csv <- "name,zipcode,phone\nAlice,01234,5551234567\nBob,00501,5559876543" df <- read_csv(csv, col_types = cols(zipcode = col_character(), phone = col_character())) print(df)

  

Medium (5-7)

Exercise 5: Skip Header Rows

library(readr) messy <- "Report: Quarterly Sales\nGenerated: 2026-03-30\n---\nproduct,q1,q2\nLaptop,120,150\nMouse,450,500" # Skip metadata, read only the data


  
Click to reveal solution ```r
library(readr) messy <- "Report: Quarterly Sales\nGenerated: 2026-03-30\n---\nproduct,q1,q2\nLaptop,120,150\nMouse,450,500" read_csv(messy, skip = 3, show_col_types = FALSE)

  

Exercise 6: Combine Two CSVs

library(readr) library(dplyr) jan <- "date,sales\n2026-01-01,100\n2026-01-02,120" feb <- "date,sales\n2026-02-01,150\n2026-02-02,130" # Read both and combine, adding a "month" column


  
Click to reveal solution ```r
library(readr); library(dplyr) jan <- "date,sales\n2026-01-01,100\n2026-01-02,120" feb <- "date,sales\n2026-02-01,150\n2026-02-02,130" df_jan <- read_csv(jan, show_col_types = FALSE) |> mutate(month = "Jan") df_feb <- read_csv(feb, show_col_types = FALSE) |> mutate(month = "Feb") bind_rows(df_jan, df_feb)

  

Exercise 7: Select Columns on Read

library(readr) csv <- paste(capture.output(write.csv(mtcars[1:5,], row.names = FALSE)), collapse = "\n") # Read only mpg, hp, wt columns


  
Click to reveal solution ```r
library(readr) csv <- paste(capture.output(write.csv(mtcars[1:5,], row.names = FALSE)), collapse = "\n") read_csv(csv, col_select = c(mpg, hp, wt), show_col_types = FALSE)

  

Hard (8-10)

Exercise 8: Parse Dates

library(readr) csv <- "event,date\nMeeting,03/30/2026\nLunch,03/31/2026\nReview,04/01/2026" # Read with proper date parsing


  
Click to reveal solution ```r
library(readr) csv <- "event,date\nMeeting,03/30/2026\nLunch,03/31/2026\nReview,04/01/2026" df <- read_csv(csv, col_types = cols(date = col_date(format = "%m/%d/%Y"))) print(df) cat("Date class:", class(df$date), "\n")

  

Exercise 9: Read First N Rows

library(readr) csv <- paste(c("x,y", paste(1:1000, round(runif(1000),3), sep=",")), collapse="\n") # Read only the first 5 rows for a quick peek


  
Click to reveal solution ```r
library(readr) csv <- paste(c("x,y", paste(1:1000, round(runif(1000),3), sep=",")), collapse="\n") read_csv(csv, n_max = 5, show_col_types = FALSE)

  

Exercise 10: Combine CSVs with Different Columns

library(readr); library(dplyr) csv_a <- "id,name,score\n1,Alice,88\n2,Bob,76" csv_b <- "id,name,grade\n3,Carol,A\n4,David,B" # Combine into one data frame (missing columns → NA)


  
Click to reveal solution ```r
library(readr); library(dplyr) csv_a <- "id,name,score\n1,Alice,88\n2,Bob,76" csv_b <- "id,name,grade\n3,Carol,A\n4,David,B" df_a <- read_csv(csv_a, show_col_types = FALSE) df_b <- read_csv(csv_b, show_col_types = FALSE) bind_rows(df_a, df_b)

  

What's Next?