-
R Programming (4) - FunctionR Programming 2020. 3. 28. 04:31728x90
Programming with functions
Functions
Functions are a fundamental building block of R.
In R, the operations that do all the work are functions.
A function is an object in R that takes some input objects (called arguments) and returns an ouput object.
A function has a form:
function_name <- function(argument_1, argument_2, ...) { expression_1 expression_2 ... return(output) }
argument_1, arguments_2
are the names of variables as inputs for the function.function_name
is the name of function. You changefunction_name
as you want.
To call or run the function we type:
function_name(x1, x2, ...)
the value of this expression is the value of output.
- A function may have more than one
return
statement, in which case it stops after executing the first one it reaches. -
If there is no statement
return(output)
then the value returned by the function is the value of the last expression in the braces. - A function always returns a value.
- For some functions the value returned is unimportant.
- In such cases one usually omits the return statement, or returns NULL.
-
If the value returned by a function is not assigned to a variable, then it is printed.
-
The most important advantage of using a function is that once it is loaded, it can be used again and again without having to reload it.
-
The second most important use of functions is to break down a programming task into smaller logical units.
Example : Find zeros of
a2*x^2+a1*x+a0=0
quad3 <- function(a0, a1, a2) { if(a2 == 0 && a1 == 0 && a0 == 0) { roots <- NA } else if (a2 == 0 && a1 == 0 ) { roots <- NULL } else if ( a2 == 0 ) { roots <- -a0/a1 } else { discrim <- a1^2 - 4*a2*a0 if (discrim > 0) { roots <- (-a1 +c(1,-1) * sqrt(a1^2-4*a2*a0))/(2*a2) } else if (discrim == 0) { roots <- -a1/(2*a2) } else { roots <- NULL } } roots }
quad3(6, -5, 1)
## [1] 3 2
Example : n choose r
The number of ways that you can choose r things from a set of n, ignoring the order, is
n_factorial <-function(n) { n_fact <- prod(1:n) return(n_fact) } n_choose_r <- function(n, r) { n_ch_r <- n_factorial(n) / n_factorial(r) / n_factorial(n-r) return(n_ch_r) }
n_choose_r(10,6)
## [1] 210
n_choose_r(10,4)
## [1] 210
Example : Winsorised mean
k-th Winsorised mean of ordered data \(x=\{ x_1, ..., x_n \}\) is defined as
wmean <- function(x, k) { x <- sort(x) n <- length(x) x[1:k] <- x[k + 1] x[(n - k + 1):n] <- x[n - k] mean(x) }
Exmple : Swap
swap values of
x[1]
andx[2]
swap <- function(x){ y <- x[2] x[2] <- x[1] x[1] <- y return(x) } x <- c(7, 8, 9) x[1:2] <- swap(x[1:2]) x[2:3] <- swap(x[2:3])
Scope and its consequences
- Argument and variables defined within a function exist only within that function.
- If you define and use a variable x inside a function, it does not exist outside the function.
- If variables with the same name exist inside and outside a function, then they are separate and do not interact at all.
- The variable defined outside the function can be seen inside the function (provided there is not a variable with the same name defined inside).
test <- function(x) { y <- x+1 return(y) } test(1)
## [1] 2
y ### Error : Object "y" not found
test2 <- function(x) { y <- x + z return(y) } z <- 1 test2(1)
## [1] 2
z <- 2 test2(1)
## [1] 3
Optional argument and default value
- To give
argument_1
the default valuex1
we useargument_1 = x1
within the function definition. - If an argument has a default, then it may be omitted when calling the function, in which case the default is used.
test3 <- function(x = 1, y = 1, z = 1) { return(x * 100 + y * 10 + z) } test3(2, 2)
## [1] 221
test3(y = 2, z = 2)
## [1] 122
Every operation in R is function call
All work in R is done by functions. For example,
k <- 3 k
## [1] 3
This function does similar operation with above.
# `<-` is a function name `<-`(k, 4) # k <- 4 k
## [1] 4
In addition, if we have
animals <- c("cow", "dog", "cat", "pig")
and you want
animals[3]
## [1] "cat"
This is equivalent to
`[`(animals, 3)
## [1] "cat"
In the above
`[`
is a function name.Similarly,
animals[4] <- "duck"
can be achieved by
(animals <- `[<-`(animals, 4, "duck"))
## [1] "cow" "dog" "cat" "duck"
In practice, we do not use the above expression, but it is useful to know that every operation in R is a function.
Other example:
x <- 3 y <- 4 x + y
## [1] 7
# functional form of arithmetic expression, which is equal to x + y `+`(x, y)
## [1] 7
if (x > y) print("x is larger than y") else print("y is larger than x")
## [1] "y is larger than x"
# functional form of if-then expression `if`(x > y, print("x is larger than y"), print("y is larger than x"))
## [1] "y is larger than x"
Function as an object
A function in R is just another object that is assigned to a symbol.
f <- function(x, y) c(x + 1, y + 2) f(1,2)
## [1] 2 4
You can type the name of function to see the code for it.
f
## function(x, y) c(x + 1, y + 2)
Vector-based programming
- Many R functions are vectorised.
- To further facilitate vector-based programming, R provides functions that enable the vectorisation of user-defined functions.
apply
,sapply
,lapply
,tapply
, andmapply
.sapply(X, FUN)
- apply function
FUN
to every element of vectorX
.
Exmaple : Simple sapply
sapply(1:5, sqrt)
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068
which is equivalent to
sqrt(1:5)
## [1] 1.000000 1.414214 1.732051 2.000000 2.236068
Example : Simple sapply with anonymous function
Anonymous function is a function without a name. You use an anonymous function when it
728x90'R Programming' 카테고리의 다른 글
R Programming (6) - Numerical integration (0) 2020.03.28 R Programming (5) - Sophisticated data structures (0) 2020.03.28 R Programming (3) - IO (0) 2020.03.28 R Programming (2) - R basic (0) 2020.03.28 R Programming (1) - R Intro (0) 2020.03.25