Module 3 · Lesson 1 of 25/18 in the course~20 min
Module lessons (1/2)
Writing Functions and Apply Family
Lesson preview7 hands-on exercises readyCourse R · Module 3 — Functions, Apply and Descriptive Statistics
Define functions with default arguments, lexical scope, explicit/implicit return, and transformations with sapply/lapply.
What you'll learn in this lesson
- Designing pure, modular functions in R with
function(...) { ... }and default values. - Distinguish between explicit
return()and implicit return of the last evaluated expression. - Understand copy-on-modify semantics (functions do not mutate external objects) and lexical scope.
- Apply functions to entire collections with
lapply(),sapply()and the alternative typevapply().
Overview
Writing a script that works on a single vector is only useful once; encapsulating that logic in a function allows you to apply it consistently across dozens of datasets, test it in isolation and avoid copy and paste errors.
In R functions are first-class objects assigned with <-:
Sections covered in this lesson
- 1Anatomy of an R function
- 2Lexical Scope and Lazy Evaluation
- 3The
applyfamily: transform without manual cycles - 4Test yourself: guided exercises
- 5Concept check
Pro Lesson · Module 3 — Functions, Apply and Descriptive Statistics
Unlock "Writing Functions and Apply Family" and the full Course R curriculum
Define functions with default arguments, lexical scope, explicit/implicit return, and transformations with sapply/lapply.
Unlock all 7 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course R plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.