← R Programming learning pathR Programming · Module 6 of 10
Functions and control flow
Write reusable functions with conditions and loops.
📖 Key ideas
- Define: square <- function(x) { x^2 }.
- if/else for decisions; for and while loops.
- Vectorised code is usually faster than loops.
- apply family: sapply, lapply apply functions to collections.
- purrr::map is the tidyverse alternative.
💡 Real-life example: A function is a recipe card you can reuse with different ingredients.
function()Defines a function
sapplyApplies function, simplifies result
lapplyApplies function, returns list
VectorisationOperating on whole vectors