R Functions
Create a function with function(). The last expression is returned. Parameters can have defaults, and ... accepts extras.
Example
add <- function(a, b = 1) { a + b }
print(add(2)); print(add(2, 5))
sq <- function(x) x^2
print(sq(6))Exercise
Define a function.
add <- (a, b) a + b
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.