Check your understanding

Quiz

You have finished the iteration section: why vectorization beats loops, the apply family, the purrr map functions and their typed variants, iterating over several inputs, and resilient iteration with safely. 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 vectorization means

Why is prices * 1.1 preferred over a for loop that multiplies each price one at a time?

Right. Vectorized code pushes the loop into fast internal code and reads as a single intention.
It works on single numbers too; vectorization is about doing it to all of them at once.
Question 2 of 8

lapply versus sapply

What is the difference between lapply() and sapply()?

Correct. Use lapply when you want a list for sure, sapply when a tidy vector would be nicer.
It works on any list or vector of any type.
Question 3 of 8

A typed map

You want a numeric vector out of a map, guaranteed, not a list. Which purrr function fits?

Right. The typed variants like map_dbl() guarantee the output type, which catches surprises early.
map2() is for iterating over two inputs, not for fixing the output type.
Question 4 of 8

Iterating over two inputs

You have a vector of prices and a matching vector of quantities, and you want price times quantity per item. Which tool is built for this?

Correct. map2(prices, qtys, ~ .x * .y) pairs the first with the first, the second with the second, and so on.
sum() collapses to one number; you want one result per item.
Question 5 of 8

Surviving a bad element

You map a function over a list, and one element makes it error. You want the whole run to finish, capturing the failure instead of stopping. Which helper fits?

Right. safely() turns a risky function into one that always returns, so a single bad element cannot sink the run.
safely() and possibly() are made exactly for this.
Question 6 of 8

When a loop is fine

Which statement is the most honest?

Right. The goal is clear, correct code; vectorization and maps win most of the time, and loops still have their place.
They can be used anywhere.
Run it: the apply family

sapply in live R

Square each number from 1 to 6, getting a clean vector back. Run it, then change the function to function(x) x * 10.

RInteractive R
sapply(1:6, function(x) x^2)

  

sapply() applies your function to each element and simplifies the results into a single vector, no loop bookkeeping required.

Run it: a typed map

map_dbl in live R

Do the same square with purrr, but demand a numeric vector out. Run it, then try map_chr and read the error it raises about the wrong type.

RInteractive R
library(purrr) map_dbl(1:6, ~ .x^2)

  

map_dbl() returns a double vector and would stop if any result were not a number, which is how the typed variants protect you from silent surprises.

Section complete

Strong work. You can say why vectorization beats a loop, tell lapply from sapply, pick a typed map_dbl when you need a vector, reach for map2 over two inputs, use safely to survive a bad element, and judge when a plain loop is still fine. Next section: defensive code and debugging.