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.
What a negative index does
For v <- c(10, 20, 30, 40), what does v[-2] return?
v[3:4]; -2 drops only position two.Keeping the rows that match
For a numeric vector v, what does v[v > 25] return?
v > 25 makes a TRUE/FALSE vector, and indexing by it keeps the TRUE positions.What a function hands back
A function has no return() statement. What value does calling it produce?
return() is for stopping early.Default arguments
A function is defined as greet <- function(name, punct = "!"). You call greet("Sam"). What is punct inside the call?
"!", not NULL.What the pipe does
You write c(4, 9, 16) |> sqrt(). What does the pipe do here?
x |> f() is just f(x), written so a chain reads in order.|> works in plain R.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?
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.
A for loop walks through each element in turn, binding it to b and running the body once per item.
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.
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.