Functions & Control Flow
Writing reusable, intelligent R code
📖 Concept Recap
Functions are the building blocks of reusable R code. Control flow lets your code make decisions and repeat actions.
- Define functions:
my_fn <- function(arg1, arg2 = default) { body } - if/else:
if (condition) { ... } else { ... } - Vectorized if:
ifelse(condition, yes, no) - for loops:
for (item in vector) { ... } - while loops:
while (condition) { ... } - sapply(): apply a function to each element of a vector, return a vector
- lapply(): apply a function to each element, return a list
R returns the last expression in a function automatically — no explicit return() needed (though you can use it for early returns).
👀 Worked Example
A function with input validation, default arguments, and a named list return:
Exercise 1 — Grade Converter
Complete the score_to_grade() function using if/else, then test it on a vector of scores with sapply().
sapply(vector, function_name) applies the function to each element and returns a vector of results. table() counts how many times each unique value appears.Exercise 2 — Vector Descriptor
Write a function describe_vector(x) that prints: N, mean, range (min–max), standard deviation, and count of outliers (values more than 2 SD from the mean). Test it on three different numeric vectors.
sum(abs(x - mean(x)) > 2 * sd(x)). Use cat() to format output. Range: range(x) returns a 2-element vector — use range(x)[1] and range(x)[2] for min and max.Exercise 3 — FizzBuzz & Higher-Order Functions
Part A: Write fizzbuzz_r(n) that returns a character vector of length n: “FizzBuzz” for multiples of 15, “Fizz” for 3, “Buzz” for 5, otherwise the number as a string. Part B: Write run_multiple(fn, values) that applies fn to each element of values and prints the results. Use it to call fizzbuzz_r on c(10, 20, 30).
ifelse() vectorized: ifelse(x %% 15 == 0, "FizzBuzz", ifelse(x %% 3 == 0, "Fizz", ...)). For run_multiple, use for (v in values) or lapply(values, fn).Build a 4-Function Analysis Toolkit
Write and test all four functions below, then demonstrate them working together on a sample dataset.
- clean_vector(x) — removes NAs and values > 3 SD from the mean, returns cleaned vector
- compare_groups(data, group_col, value_col) — returns a data frame with mean, sd, and n per group
- flag_outliers(x) — returns a logical vector: TRUE where value is > 2 SD from mean
- full_report(x, label) — prints a formatted report using the other functions
full_report().✅ Lab 4 Complete!
You can now write reusable, intelligent R functions with input validation, default arguments, control flow, and the apply family. These skills transform scripts into maintainable analysis tools.
Continue to Lab 5: String Manipulation →