- 01Setting up (what you'll need)
- 02Your first output (print)
- 03Using variables
- 04Branching (the if statement)
- 05Repeating (the for loop)
- 06Writing your own function
- 07Working with vectors
- 08Repeating (the while loop)
- 09Structured data with lists
- 10Error handling (tryCatch)
- 11Sorting a vector
- 12Recursive functions
- 13Using the handy built-in functions
- 14The switch() function
- 15Applying a condition across a vector with ifelse()
- 16Applying a function across data with sapply()
- 17Writing comments
- 18Logical operators (&&, ||, !)
- 19Constants (the upper-case convention)
- 20Splitting and joining strings (strsplit and paste)
- 21Writing NULL-safe code
- 22Searching a vector (%in%)
- 23Transforming a vector with sapply (the map equivalent)
- 24Tabular data with matrices
- 25Creating your own condition
- 26Default arguments
- 27Set operations (union, intersect, setdiff)
- 28Checking correctness with stopifnot (a first step towards testing)
- 29Higher-order functions (passing a function as an argument)
- 30Stacks and queues (the basics of data structures)
- 31The basics of type conversion (casting)
- 32An introduction to regular expressions (pattern matching)
- 33The binary search algorithm
- 34Building a Caesar cipher (a letter-shifting cipher)
- 35Understanding how bubble sort works
- 36Assembling and displaying a date
- 37Writing several tests together (multiple test cases)
- 38Speeding up calculation with memoization (caching)
- 39Normalizing strings (trimws and case)
- 40Shallow copies versus deep copies
- 41factor, R's equivalent of an enum
- 42Flattening a list
- 43Reversing a string and testing for a palindrome
- 44Pairing up two vectors (the zip operation)
- 45Rounding numbers (floor, ceiling, round)
- 46Assembling multi-line text
- 47Returning several values from a function (lists)
- 48Finding the GCD and LCM (the Euclidean algorithm)
- 49Formatting numbers (aligning digits and decimal places)
- 50Cleaning up with tryCatch's finally
- 51Type-agnostic general functions
- 52The basics of reading and writing files
- 53Generating random numbers
- 54Bitwise operations (bitwAnd, bitwOr, bitwXor, shifts)
- 55Receiving command-line arguments
- 56Pausing for a set time (Sys.sleep)
- 57Watch out for floating-point error
- 58Reading a value from standard input
- 59FizzBuzz (the classic exercise)
- 60Testing whether a number is prime
- 61Set operations (union, intersection, difference)
- 62Converting between bases (binary and hexadecimal)
- 63Checking that brackets match (an application of stacks)
- 64Testing whether two words are anagrams
- 65Testing whether a year is a leap year
- 66Converting temperatures (Celsius and Fahrenheit)
- 67Finding the prime factors
- 68[Project] Build a simple inventory system
Higher-order functions (passing a function as an argument)
This lesson covers passing a function as an argument in R, with the aim of treating the work itself as a component.
Being able to pass one function into another is what makes it a higher-order function. Picture specifying only how the work should be done and leaving the doing to someone else. Passing an aggregation rule to Reduce() is the classic example.
The sample code totals a vector by passing the aggregation rule as a function, as in Reduce(function(a, b) a + b, nums). The same idea carries over to other higher-order functions such as filter and Map.
A common early stumble is simply getting used to anonymous function notation. Until function(a, b) a + b feels natural, the difference from an ordinary definition is confusing.
In professional work, it extends to designs where the logic is swapped out depending on the situation — an important part of what makes R's data handling work.
๐งช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
