




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Function in R Programming.Introduce.Recursion.
Typology: Slides
1 / 36
This page cannot be seen from the preview
Don't miss anything!
Functions
Biostatistics 140.
Functions are created using the function() directive and are stored as R objects just like anything else. In particular, they are R objects of class “function”.
f <- function(
}
Functions in R are “first class objects”, which means that they can be treated much like any other R object. Importantly, Functions can be passed as arguments to other functions Functions can be nested, so that you can define a function inside of another function The return value of a function is the last expression in the function body to be evaluated.
R functions arguments can be matched positionally or by name. So the following calls to sd are all equivalent
mydata <- rnorm(100) sd(mydata) sd(x = mydata) sd(x = mydata, na.rm = FALSE) sd(na.rm = FALSE, x = mydata) sd(na.rm = FALSE, mydata)
Even though it’s legal, I don’t recommend messing around with the order of the arguments too much, since it can lead to some confusion.
You can mix positional matching with matching by name. When an argument is matched by name, it is “taken out” of the argument list and the remaining unnamed arguments are matched in the order that they are listed in the function definition.
args(lm) function (formula, data, subset, weights, na.action, method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, contrasts = NULL, offset, ...)
The following two calls are equivalent.
lm(data = mydata, y ~ x, model = FALSE, 1:100) lm(y ~ x, mydata, 1:100, model = FALSE)
Function arguments can also be partially matched, which is useful for interactive work. The order of operations when given an argument is (^1) Check for exact match for a named argument (^2) Check for a partial match (^3) Check for a positional match
f <- function(a, b = 1, c = 2, d = NULL) {
In addition to not specifying a default value, you can also set an argument value to NULL.
Another example
f <- function(a, b) { print(a) print(b) }
f(45) [1] 45 Error in print(b) : argument "b" is missing, with no defaul
Notice that “45” got printed first before the error was triggered. This is because b did not have to be evaluated until after print(a). Once the function tried to evaluate print(b) it had to throw an error.
The ... argument indicate a variable number of arguments that are usually passed on to other functions. ... is often used when extending another function and you don’t want to copy the entire argument list of the original function myplot <- function(x, y, type = "l", ...) { plot(x, y, type = type, ...) } Generic functions use ... so that extra arguments can be passed to methods (more on this later).
mean function (x, ...) UseMethod("mean")
One catch with ... is that any arguments that appear after ... on the argument list must be named explicitly and cannot be partially matched.
args(paste) function (..., sep = " ", collapse = NULL)
paste("a", "b", sep = ":") [1] "a:b"
paste("a", "b", se = ":") [1] "a b :"
How does R know which value to assign to which symbol? When I type
lm <- function(x) { x * x } lm function(x) { x * x }
how does R know what value to assign to the symbol lm? Why doesn’t it give it the value of lm that is in the stats package?
The global environment or the user’s workspace is always the first element of the search list and the base package is always the last. The order of the packages on the search list matters! User’s can configure which packages get loaded on startup so you cannot assume that there will be a set list of packages available. When a user loads a package with library the namespace of that package gets put in position 2 of the search list (by default) and everything else gets shifted down the list. Note that R has separate namespaces for functions and non-functions so it’s possible to have an object named c and a function named c.
The scoping rules for R are the main feature that make it different from the original S language. The scoping rules determine how a value is associated with a free variable in a function R uses lexical scoping or static scoping. A common alternative is dynamic scoping. Related to the scoping rules is how R uses the search list to bind a value to a symbol Lexical scoping turns out to be particularly useful for simplifying statistical computations
Lexical scoping in R means that the values of free variables are searched for in the environment in which the function was defined.
What is an environment? An environment is a collection of (symbol, value) pairs, i.e. x is a symbol and 3.14 might be its value. Every environment has a parent environment; it is possible for an environment to have multiple “children” the only environment without a parent is the empty environment A function + an environment = a closure or function closure.
Searching for the value for a free variable: If the value of a symbol is not found in the environment in which a function was defined, then the search is continued in the parent environment. The search continues down the sequence of parent environments until we hit the top-level environment; this usually the global environment (workspace) or the namespace of a package. After the top-level environment, the search continues down the search list until we hit the empty environment. If a value for a given symbol cannot be found once the empty environment is arrived at, then an error is thrown.