Check your understanding

Quiz

You have finished the structures section: lists and nested data, data frames and tibbles, inspecting an object with str(), matrices and arrays, and converting a column's type on purpose. 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

Single bracket, double bracket

You have a list x. What is the difference between x[1] and x[[1]]?

Right. Single bracket keeps the list box; double bracket reaches inside and hands you the contents.
[[ ]] works on any list element, of any type.
Question 2 of 8

What a data frame is

Which statement best describes a data frame?

Correct. That is why one column can be numbers and another text, while every column shares the same number of rows.
Columns can be any type, including text and factors.
Question 3 of 8

A tibble is a data frame, plus

A tibble differs from a base data.frame in which way?

Right. Tibbles also keep subsetting predictable, returning a tibble rather than dropping to a vector.
Tibbles are the native data frame of the tidyverse.
Question 4 of 8

What str() is for

You are handed an unfamiliar object. What does str(object) show you?

Correct. str() is the fastest way to see what something actually is before you work with it.
str() prints text, not a chart.
Question 5 of 8

Matrix versus data frame

You need a structure where every cell is the same type, laid out in rows and columns. Which fits?

Right. A matrix is one type throughout; reach for it when the whole grid is numbers, say.
A plain vector has no row-and-column layout.
Question 6 of 8

Converting text to numbers

You run as.numeric(c("12", "3.5", "abc")). What comes back?

Correct. The convertible strings become numbers; "abc" becomes NA and you get a warning.
Conversion happens; only the genuinely non-numeric value fails.
Run it: reach into a list

Lists in live R

Build a small list with named parts, then pull pieces out two ways. Run it, then try person["name"] (single bracket) and compare what it returns to person[["name"]].

RInteractive R
person <- list(name = "Mara", scores = c(88, 91, 79), active = TRUE) person[["name"]] # the element itself person$scores # $ also reaches inside, by name mean(person$scores)

  

[[ ]] and $ both reach inside the list and hand you the actual element, here a string and a numeric vector you can take the mean of.

Run it: inspect a data frame

str() in live R

Build a tiny data frame with two types of column, then inspect its structure. Run it, then add a third column of your own and run str() again.

RInteractive R
shop <- data.frame(item = c("pen", "mug", "book"), price = c(2.5, 8.0, 14.0)) str(shop) nrow(shop)

  

str() shows you the columns, their types (chr and num here), and a preview, all at a glance, which is how you orient yourself in unfamiliar data.

Section complete

Well done. You can reach into a list with [[ ]] and $, describe a data frame as equal-length typed columns, say what a tibble adds, read an object's shape with str(), choose a matrix when everything is one type, and convert types on purpose while knowing what produces an NA. Next section: subsetting, control flow and writing your own functions.