Check your understanding

Quiz

You have finished the import section: reading CSV and delimited files, Excel and other formats, JSON and web data, databases and big files, and saving your results back out. This short quiz checks what stuck. Pick an answer to continue; you can retry until it clicks. The last two steps are live R, run them and tinker.

Question 1 of 8

What read_csv gives you

You read a file with readr::read_csv(). What kind of object comes back?

Right. read_csv() returns a tibble and tells you the type it guessed for each column.
It returns the data so you can assign and use it.
Question 2 of 8

A number that arrived as text

A price column you expected to be numeric imports as text. What is the most likely cause?

Yes. One stray $ or , is enough for the reader to treat the whole column as text.
A clean numeric column imports as a number.
Question 3 of 8

Reading an Excel workbook

You need to read a .xlsx workbook into R. Which package is the standard choice?

Correct. readxl::read_excel() reads both .xls and .xlsx workbooks.
Base R has no Excel reader; you need a package.
Question 4 of 8

Two ways to save

You want to save a data frame and read it back later with its exact column types intact, including factors and dates. Which fits best?

Right. saveRDS() round-trips the object faithfully; reach for CSV when you need a file other tools can read.
They can, in several formats.
Question 5 of 8

Who does the work in a database query

You query a large database table with dbplyr. Where does the filtering and summarising actually happen?

Correct. That is the whole point: the database does the heavy lifting and R receives only the small answer.
Excel is not involved; the query runs in the database.
Question 6 of 8

Garbled accented characters

A column of names with accents imports as garbled symbols. What is the usual culprit?

Right. Tell the reader the file's encoding, often UTF-8, so the accented bytes are decoded correctly.
Other columns do not affect how text is decoded.
Run it: write then read a CSV

A CSV round-trip in live R

Save a small data frame to a temporary CSV, then read it straight back. Run it, then add a column to shop and run again.

RInteractive R
shop <- data.frame(item = c("pen", "mug", "book"), price = c(2.5, 8.0, 14.0)) path <- tempfile(fileext = ".csv") write.csv(shop, path, row.names = FALSE) read.csv(path)

  

You wrote the frame out as text and read it back as a data frame, the core of every import: out to a file, then back into R.

Run it: save with types intact

saveRDS preserves the object

Save a data frame with a factor column, read it back, and confirm the types survived. Run it and read the str() output.

RInteractive R
orig <- data.frame(grade = factor(c("A", "B", "A")), n = c(3L, 5L, 2L)) path <- tempfile(fileext = ".rds") saveRDS(orig, path) back <- readRDS(path) str(back)

  

str(back) shows the Factor and integer types came back exactly as they went out, which CSV could not have guaranteed.

Section complete

Well done. You can read CSV with read_csv and know why a column might arrive as text, pick readxl for Excel, choose between write_csv and saveRDS for saving, reason about where a database query runs, and spot an encoding problem. Next section: strings, dates and regular expressions.