Data Science Desktop Survival Guide
by Graham Williams |
|||||
Functions |
# Add two numbers.
sum(1, 2)
Here the function has returned a single value, though it is technically returned as a vector of length 1 containing just the item 3. The result can be stored into a variable as discussed in Section 3.18. We can define out own functions: |
mysum <- function(x) x + 1
A shorthand for function() introduced in 2021 is: |
mysum <- \(x) x + 1
This syntax is most useful for anonymous functions inline. |
sapply(1:10, \(x) x + 1)
|