In R, functions are "first class" objects.
g = function(x) {
sin(x) * exp(-0.01*x) # not: y = ...
} # but: return(y)
g
g(1)
The last evaluated expression is returned.
NOTE: Assignments do not return a value!
R supports "local scoping" rules:
a = 0.05
g = function(x) sin(x) * exp(-a*x)
g(1)
a = 0.10
g(1)
Keywords with default values are allowed.
g = function(x, a=0.1)
return(sin(x) * exp(-a*x))
As objects, functions can be defined within functions. And they can be used as variables for other functions.
curve(g, 0, 6*pi, col=4)
grid()
NOTE: Some functions, such as curve()
or integrate()
require the function to be vectorized.
integrate(g, 0, 2*pi, a=0.1)
Variables are passed as values, not as pointers, thus they cannot be changed with the function.
As a consequence: more memory will be needed, especially when a variable is a big dataframe.
[copy-by-demand/change + laziness]
When several return values shall be returned, a list will be needed -- multiple assignments are not possible.
min_ind <- function(x) {
m <- min(x)
i <- (1:length(x))[x == m]
list(min=m, inds=i)
}
x = c(4,5,6,3,7,8,3,3,9)
res = min_ind(x)
cat("The minimum is", res$min, "and the indices are:", res$inds, "\n")
for (i in 1:n) { while (...) { if (...) {
... ... ...
} } } else {
...
for (a in vec) { repeat { }
... ...
} break
}
mySqrt = function(a, n=2) {
x0 = Inf
x1 = 1
while (abs(x1-x0) > 1e-15) {
x0 = x1
x1 = x0 - (x0^n - a) / n / x0^(n-1)
}
x1
}
mySqrt(2, 5)
2^0.2
apply
Functions¶A = matrix(1:12, 4, 3)
A
apply(A, 1, mean)
apply(A, 2, mean)
X = list(x=1, y=c('a','b','c'), z=rep(0,100))
lapply(X, length)
sapply(X, length)
minind <- function(x) {
m <- min(x)
i <- (1:length(x))[x == m]
r = list(min=m, inds=i)
class(r) <- "minind"
r
}
print.minind <- function(r) {
cat("The minimum is", res$min, "and the indices are:", res$inds, "\n")
}
x <- c(4,5,6,3,7,8,3,3,9)
res = minind(x)
res
str(res)
For debugging purposes, it is best to use the features RStudio is providing.
Also:
options(error=recover) # options(error=NULL)
trace(fun) # untrace(fun)
debug(fun) # undebug(fun), also: debugonce(fun)
setBreakpoint(file, line) # clear=TRUE
These functions will call the browser: browse