## Learn How to Create User Defined Functions in R

### Introduction to User Defined Functions in R

One can create user defined functions in R Language easily. User-defined functions allow to write/create custom blocks of code to be reused throughout the analysis. The article presents some useful examples of how to write user defined functions in R Language. R language helps to create much more efficient and possibly elegant coding.

### Assigning Function to a Variable

Example 1: Create a simple function and assign the function to a variable name as we do with any other objects.

f <- function(x, y = 0){
z <- x + y
z
}

x = rnorm(10)
f(x + y)

### Regression Coefficients

Example 2: Given $n\times 1$ vector of $y$ and matrix of $X$ the $\hat{\beta}=E[X|y] = (X’X)^{-1}X’y$, where $(X’X)^{-1}$ is generalized invers of $X’X$.

Beta <- function(x, y){
X <- qr(x)
qr.coef(X, y)
}

attach(mtcars)
xmat = cbind(1, hp, wt)
yvar = mpg
regcoef <- Beta(xmat, yvar)

The qr() function computes the QR decomposition of a matrix. The QR decomposition if used to solve the equation $Ax=b$ for a given matrix $A$, and vector $b$. It is very useful in computing regression coefficients and in applying Newton Raphson’s algorithm.

### Removing all Objects from globalenv

Example 3: Create a function capable of removing all objects from the globalenv.

clear <- function(env = globalenv() ){
obj = ls(envir = env)
rm(list = obj, envir = env)
}

The clear() function removes all objects from a specified environment and seems to work correctly. However, the clear() function detects also itself and as a result, it cannot be reused without redefining the function again.

The clear() function can be improved to keep the function clear() when all other objects are deleted.

clear <- function(env = globalenv()){
objects <- objects(env)
objects <- objects[objects != "clear"]
rm(list = objects, envir = env)
invisible(NULL)
}

### Computing Measure of Central Tendency

Example 4: Create a function that can compute some basic Measure of Central Tendency.

center = function(x, type){
switch(type,
mean = mean(x),
median = median(x),
trimmed = mean(x, trim = 0.1))
}

attach(airquality)
center(Temp, "mean")     # for calcualtion of mean
center(Temp, "median")   # for calculation of median
center(Temp, "trimmed")  # for calculation of trimmed mean

Note that the user-defined functions in R can incorporate conditional statements, loops, and other functionalities to perform more advanced tasks. They can also have default parameter values for added flexibility.

https://itfeature.com

https://gmstat.com

## Components of Functions in R Language

### Components of Function in R Language

Functions in the R Language are objects with three basic components. The components of functions in R language are:

Let us discuss the components of functions in R language in detail.

#### Formal Argument in R

To learn about Formal argument in R language, see the post formal argument, also see the basics about functions in R Language.

#### Body of a Function

The body of a function is parsed R statements. The body of a function is usually a collection of statements in braces but it can be a single statement, a symbol, or even a constant.

#### Environment of a Function

The environment of a function is the environment that was active at the time that the function was created. The environment of a function is a structural component of the function and belongs to the function itself.

A fourth component of a function in R language can be considered as a “Return Value” by a function.

#### Return Value of a Function

The last object called within a function is returned by the function and therefore available for assignment. Functions can return only a single value but in practice, this is not a limitation as a list containing any number of objects can be returned. Objects can be returned visible or invisible. This option does not affect the assignment side but affects the way results are displayed when the function is called.

y <- function(n){
out <- runif(n)
invisible(out)
}

### Functions Closures in R

A function closure or closure is a function together with a referencing environment. Almost all functions in R are closures as they remember the environment where they were created. The functions that cannot be classified as closures, and therefore do not have a referencing environment, are known as primitives.

In R, internal functions are called the underlying C code. These sum() and c() are good cases in point:

environment(sum)

When we call a function, a new environment is created to hold the function’s execution, and normally, that environment is destroyed when the function exists. But, if we define a function g() that returns a f(), the environment where f() is created is the execution environment of g(), that is, the execution environment of g() is the referencing environment of f(). As a consequence, the execution environment of g() is not destroyed as g() exists but it persists as long as f() exists. Finally as f() remembers all objects bound to its referencing environment f() remembers all objects bound to the execution environment of g().

We can use the referencing environment of f(), to hold any of the objects and these will be available to f().

g <- function(){
y <- 1
function(x){
x + y
}

f1 <- g()

print(f1)

f1(3)

Closures can be used to write functions that in turn closures can be used to generate new functions. This allows us to have a double layer of development: a first layer that is used to do all the complex work in common to all functions and a second layer that defines the details of each function.

f <- function(i){
function(x){
x+i
}
}
f1 <- f(1)
f2 <- f(2)
f2(4)

By understanding these components, one can effectively create and use functions to enhance one’s R programming.

Note that in R language:

• Functions can be nested within other functions.
• Functions can access variables from the environment where they are defined (lexical scoping).
• R provides many built-in functions for various tasks.
• One can create customized functions to automate repetitive tasks and improve code readability.

https://itfeature.com

https://rfaqs.com

https://gmstat.com

## Formal Arguments in R: Quick Guide

### Introduction to Formal Arguments in R

Formal arguments in R are essentially variables you define within a function’s code block. These arguments act as placeholders for the data that are provided when a user uses the function. Formal are the formal arguments of function returned as an object of class pairlist which can be thought of as something similar to a list with an important difference:

is.null(pairlist())
is.null(list())

That is a pairlist of length zero is NULL while a list is not.

### Specifying Formal Arguments Positions

Formal arguments in R can be specified by position or by name and we can mix positional matching with matching by name. The following are equivalent.

mean(x = 1:5, trim = 0.1)
mean(1:5, trim = 0.1)
mean(x = 1:5, 0.1)
mean(1:5, 0.1)
mean(trim = 0.1, x = 1:5)

### Functions Formals Default Values

Functions formals may also have the construct symbol=default, which unless differently specified, forces any argument to be used with its default value. Specifically, functions mean() also have a third argument na.rm that defaults to FALSE and as a result, passing vectors with NA values to mean() returns NA.

mean(c(1, 2, NA))

while by specifying na.rm=TRUE we get the mean of all non-missing elements of vector x.

mean(c(1, 2, NA), na.rm = TRUE)

we can redefine mean() function that defaults na.rm to TRUE by simply

mean(c(1, 2, NA))

Now we have a copy of mean.default() in our globalenv:

exists("mean.default", envir = globaenv())

also, notice

environment(mean.default)

### The … Argument in a Function

The … argument of a function is special and can contain any number of symbol=value arguments. The … argument is transformed by R into a list that is simply added to the formal list:

h<-function(x, …){
0
}

formals(h)

The … argument can be used if the number of arguments is unknown. Suppose one wants to define a function that counts the number of rows of any given number of data frames. One can write:

count_rows<-function(…){
list<-list(…)
lapply(list, nrow)
}

count_rows(airquality, cars)

By effectively using formal arguments in R Language, one can create reusable and adaptable functions that make the R code more concise and efficient.

https://itfeature.com

https://gmstat.com

## Lexical Scoping in R Language

### Introduction to Lexical Scoping

The Lexical Scoping in R Language is the set of rules that govern how R will look up the value of a symbol. For example

x <- 10

In this example, scoping is the set of rules that R applies to go from symbol $x$ to its value 10.

### Types of Scoping

R has two types of scoping

1. Lexical scoping: implemented automatically at the language level
2. Dynamic scoping: used in select functions to save typing during interactive analysis.

Lexical scoping looks up symbol values based on how functions were nested when they were created, not how they are nested when they are called to figure out where the values of a variable will be looked up. You just need to look at the function’s definition.

### Basic Principles of Lexical Scoping in R Language

There are four basic principles behind R’s implementation of lexical scoping in R Language:

The following example will illustrate the basic principle of lexical scoping

f <- function(){
x <- 1
y <- 2
c(x, y)
}

f()

If a name is not defined inside a function, R will look one level up.

x <- 2
g <- function(){
y <- 1
c(x,y)
}
g()

The same rules apply if a function is defined inside another function: look inside the current function, then where the function was defined, and so on, all the way up to the global environment, and then on to other loaded packages.

x <- 1
h <- function(){
y <- 2
i <- function(){
z <- 3
c(x,y,z)
}
i()
}

h()
r(x,y)

The same rules apply to closures, functions created by other functions. The following function, j( ), returns a function.

How does R know what the value of y is after the function has been called? It works because k preserves the environment in which it was defined and because the environment includes the value of y.

j <- function(x){
y <- 2
function(){
c(x,y)
}
}
k<-j(1)
k()
rm(j,k)

#### Functions vs Variables

Finding functions works the same way as finding variables:

l <- function(x){
x+1
}
m <- function(){
l <- function(x){
x*2
}
l(10)
}
m()

If you are using a name in a context where it’s obvious that you want a function (e.g. f(3)), R will ignore objects that are not functions while it is searching. In the following example, n takes on a different value depending on whether R is looking for a function or a variable.

n <- function(x) {
x/2
}
o <- function(){
n <- 10
n(n)
}
o()

#### Fresh Start

The following questions can be asked (i) What happens to the values in between invocation of a function? (ii) What will happen the first time you run this function? and (iii) What will happen the second time? (If you have not seen exists() before it returns TRUE if there’s a variable of that name, otherwise it returns FALSE).

j <- function(){
if(!exists("a")) {
a <- 1
} else {
a<-a+1
}
print(a)
}
j()

From the above example, you might be surprised that it returns the same value, 1 every time. This is because every time a function is called, a new environment is created to host execution. A function has no way to tell what happened the last time it was run; each invocation is completely independent (but see mutable states).

#### Dynamic Lookup

Lexical scoping determines where to look for values, not when to look for them. R looks for values when the function is run, not when it’s created. This means that the output of a function can be different depending on objects outside its environment:

f <- function() {
x
}
x <- 15
f()

x <- 20
f()

You generally want to avoid this behavior because it means the function is no longer self-contained.
One way to detect this problem is the findGlobals() function from codetools. This function lists all the external dependencies of a function:

f <- function{
x + 1
}
codetools::findGlobals(f)

Another way to try and solve the problem would be to manually change the environment of the function to the emptyenv(), an environment that contains absolutely nothing:

environment(f) <- emptyenv()

This doesn’t work because R relies on lexical scoping to find everything, even the + operator. It’s never possible to make a function completely self-contained because you must always rely on functions defined in base R or other packages.

Since all standard operators in R are functions, you can override them with your alternatives.

'(' <- function(e1) {
if(is.numeric(e1) && runif(1)<0.1){
e1 + 1
} else {
e1
}
}
replicate (50,(1+2))

A pernicious bug is introduced: 10% of the time, 1 will be added to any numeric calculation inside parenthesis. This is another good reason to regularly restart with a clean R session!

### Bound Symbol or Variable

If a symbol is bound to a function argument, it is called a bound symbol or variable. In case, if a symbol is not bound to a function argument, it is called a free symbol or variable.

If a free variable is looked up in the environment in which the function is called, the scoping is said to be dynamic. If a free variable is looked up in the environment in which the function was originally defined the scoping is said to be static or lexical. R, like Lisp, is lexically scoped whereas R and S-plus are dynamically scoped.

y = 20
foo = function(){
y = 10  #clouser for the foo function
function(x) {
x + y
}
}
bar=foo()

Foo returns an anonymous function.

bar=foo() is a function in global like foo. $x + y$ is created in the foo environment not in global. Foo has a function as a return value, which is then bound to bar the global environment. Note that anonymous is a function that has no name.

https://itfeature.com

https://gmstat.com