Error in `read_csv()`:
! could not find function "read_csv"

Control Flow
Grayson White
Math 241
Week 9 | Spring 2026
R programming skills: control flow, functions, iteration (loops and functional programming)
The flow of execution of R code.
So far, our primary rule: Place your code in the order that you want R to evaluate it.
Incorrect order:
Can also get R to conditionally run code blocks or run code blocks multiple times.
Control structures allow you to control the flow of execution.
Main tools we will use to control the flow:
if() and else()stop() and stopifnot()for() loopsAnd operators:
& and |) vs. single-element comparison (&& and ||)Element-wise comparison:
&& and || in our activity todayNot operator:
Note the use of backticks.
This works for all operators!
R will conditionally execute code via if statements.
The basic one-line form is:
The basic multi-line form is:
R will conditionally execute code via if statements.
if() is not vectorized!
Error in `if (x == 1) ...`:
! the condition has length > 1
Error in `if (x == 3) ...`:
! the condition has length > 1
if() is not vectorized! Alternative condition?
if() is not vectorized! Alternative condition?
if() is not vectorized! Alternative condition?
Error in `if (x %in% 3) ...`:
! the condition has length > 1
Because if is not vectorized, it is often helpful to collapse logical vectors to a single value.
any() and all()if() else()ifelse() runs operations on an entire vector.Alternative for more informative error messages:
if()stop() if they are wrong.The flow of execution of R code.
Order still matters.
But now we can:
if() and else().stop().Very helpful for building functions (Wednesday)
Will add in for() loops and other iteration methods next week.
Also need to weave in best practices.