Check your understanding

Quiz

You have finished the programming section: subsetting and replacement, if and loops, writing your own functions, default and named arguments, the pipe, and how R resolves names. 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 a negative index does

For v <- c(10, 20, 30, 40), what does v[-2] return?

Correct. In R a negative index means "drop this position and keep the rest".
That would be v[3:4]; -2 drops only position two.
Question 2 of 8

Keeping the rows that match

For a numeric vector v, what does v[v > 25] return?

Right. v > 25 makes a TRUE/FALSE vector, and indexing by it keeps the TRUE positions.
The comparison gives one logical per element, and indexing returns the matching values.
Question 3 of 8

What a function hands back

A function has no return() statement. What value does calling it produce?

Correct. R returns the final expression automatically; return() is for stopping early.
The return value is whatever the body computes last, not an argument by default.
Question 4 of 8

Default arguments

A function is defined as greet <- function(name, punct = "!"). You call greet("Sam"). What is punct inside the call?

Right. A default fills in when the caller omits that argument, so the function still runs.
The default is "!", not NULL.
Question 5 of 8

What the pipe does

You write c(4, 9, 16) |> sqrt(). What does the pipe do here?

Correct. x |> f() is just f(x), written so a chain reads in order.
The base pipe |> works in plain R.
Question 6 of 8

What scope protects

Inside a function you write x <- 99, where x is also a global variable equal to 1. After the function runs, what is the global x?

Right. Assignments inside a function are local, which is what keeps functions from quietly corrupting your workspace.
Same-named variables in different scopes coexist without error.
Run it: a for loop

Loops in live R

Loop over a vector and print a line per item. Run it, then change the message or the vector and run again.

RInteractive R
books <- c("Dune", "Solaris", "Foundation") for (b in books) { cat("Reading:", b, "\n") }

  

A for loop walks through each element in turn, binding it to b and running the body once per item.

Run it: write a function

Functions in live R

Define a small function with a default argument, then call it both ways. Run it, then add a check at the top with if.

RInteractive R
late_fee <- function(days, rate = 0.25) { days * rate } late_fee(4) # uses the default rate late_fee(4, rate = 0.5) # overrides it

  

The default rate = 0.25 fills in when you omit it, and a named argument lets you override just the one you mean.

Section complete

Strong work. You can subset with negative and logical indices, predict what a function returns, rely on default and named arguments, read a |> chain in order, and trust that a function's local assignments leave your globals alone. Next section: importing and exporting real data.