4. Functions
Functions
are used to create cohesive, reusable units of code. The best way to learn how to create functions is by looking at a few simple ones and then start creating your own. Note that functions are created by using the function
keyword and they also have parameters
or arguments
. Parameters or arguments are simply inputs to the function. You may pass inputs to a function by position
or by name
. Futhermore, a function may return
a value or not.
Here is a function that accepts a single argument and returns nothing. Its sole job is to print the input given.
[1]:
printIt <- function(x) {
print(x)
}
printIt(3)
printIt('hello, world!')
[1] 3
[1] "hello, world!"
Here is a function that accepts 2 inputs x
and y
, adds them, and returns the result.
[2]:
add <- function(x, y) {
result <- x + y
return(result)
}
a <- 3
b <- 4
c <- add(a, b)
d <- add(x=10, y=11)
[3]:
print(c)
[1] 7
[4]:
print(d)
[1] 21
Note that if your function is very simple (fits on one line concisely), you may define a function as follows.
[5]:
add <- function(x, y) x + y
a <- add(10, 11)
print(a)
[1] 21
4.1. Optional arguments
You may provide default values for arguments; if you do so, those arguments are optional
(a user does not have to pass them in).
[6]:
add <- function(x, y=13) x + y
a <- add(10)
print(a)
[1] 23
4.2. Functions as arguments
You may even pass functions as arguments! R
is a functional programming language where functions are no different than data or variables. Below, we have four functions.
add
which adds two numbersx
andy
and returns the numbers addedsubtract
which adds two numbersx
andy
and returns the numbers subtractedmultiply
which adds two numbersx
andy
and returns the numbers multiplieddivide
which adds two numbersx
andy
and returns the numbers divided
We also have a function doIt
which takes a function f
and two parameters x
and y
. Whatever f
is, we pass in x
and y
as arguments to it.
[7]:
add <- function(x, y) x + y
subtract <- function(x, y) x - y
multiply <- function(x, y) x * y
divide <- function(x, y) x / y
doIt <- function(f, x, y) f(x, y)
a <- doIt(add, 10, 8)
b <- doIt(subtract, 10, 8)
c <- doIt(multiply, 10, 8)
d <- doIt(divide, 10, 8)
[8]:
print(a)
[1] 18
[9]:
print(b)
[1] 2
[10]:
print(c)
[1] 80
[11]:
print(d)
[1] 1.25
4.3. Built-in functions
R
has a lot of built-in functions that you may reuse. Here, we sample of few of the ones that will be most important for your data analysis and science activities.
4.3.1. sum
The sum
function adds a sequence of numbers.
[12]:
a <- sum(1, 2, 3)
print(a)
[1] 6
4.3.2. round
The round
function rounds a number to the precision specified.
[13]:
a <- round(3.1415, 3)
print(a)
[1] 3.142
4.3.3. toupper
The toupper
function upper-cases all letters.
[14]:
a <- toupper('hello, world!')
print(a)
[1] "HELLO, WORLD!"
4.3.4. seq
The seq
function returns a list of numbers in the specified range (inclusive both ends).
[15]:
a <- seq(1, 10)
print(a)
[1] 1 2 3 4 5 6 7 8 9 10
4.3.5. runif
The runif
function (random uniform) samples number from the range [0, 1] uniformly.
[16]:
a <- runif(5)
print(a)
[1] 0.59439448 0.55153470 0.83145848 0.07751116 0.35225127
4.3.6. rnorm
The rnorm
function (random normal) samples numbers from a normal distribution.
[17]:
a <- rnorm(5)
print(a)
[1] -1.67525560 -0.04191879 -0.23720700 0.68331315 -0.88037688
4.3.7. mean
The mean
function computes the average of a sequence of numbers.
[18]:
a <- mean(seq(1, 9))
print(a)
[1] 5
4.3.8. sd
The sd
function computes the standard deviation of a sequence of numbers.
[19]:
a <- sd(seq(1, 9))
print(a)
[1] 2.738613
4.3.9. which.max
The which.max
function returns the index of the largest element of a list of numbers.
[20]:
a <- which.max(seq(1, 9))
print(a)
[1] 9
4.3.10. which.min
The which.min
function returns the index of the smallest element of a list of numbers.
[21]:
a <- which.min(seq(1, 9))
print(a)
[1] 1
4.3.11. sort
The sort
function sorts the elements in a list.
[22]:
a <- c(5, 4, 3, 2, 1)
b <- sort(a)
print(a)
print(b)
[1] 5 4 3 2 1
[1] 1 2 3 4 5
4.3.12. cut
The cut
function creates factors with specified cutpoints. The cut
function essentially discretizes
the data and produces factors from numeric data.
[23]:
a <- seq(1, 20, 2)
b <- cut(a, c(0, 5, 10, 15, 20))
print(b)
[1] (0,5] (0,5] (0,5] (5,10] (5,10] (10,15] (10,15] (10,15] (15,20]
[10] (15,20]
Levels: (0,5] (5,10] (10,15] (15,20]
4.3.13. choose
The choose
function computes the number of k
combinations from n
elements.
[24]:
a <- choose(10, 2)
print(a)
[1] 45
4.3.14. unique
The unique
function returns a list of unique elements in another list.
[25]:
a <- unique(c(1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5))
print(a)
[1] 1 2 3 4 5
4.3.15. subset
The subset
function returns a subset list of elements in another satisfying a comparison.
[26]:
a <- seq(1, 20)
b <- subset(a, a < 10)
print(b)
[1] 1 2 3 4 5 6 7 8 9
4.3.16. table
The table
function returns frequencies of elements in a list.
[27]:
a <- table(c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5))
print(a)
1 2 3 4 5
5 4 3 2 1
4.3.17. sample
The sample
function samples with or without replacement from a list of elements.
[28]:
a <- sample(seq(1, 10), 5, replace=TRUE)
print(a)
[1] 4 1 1 6 6
[29]:
a <- sample(seq(1, 10), 5, replace=FALSE)
print(a)
[1] 2 8 3 7 6
4.3.18. rep
The rep
function creates a vector of the same elements as many times as specified. Below, we create a vector having five 10.
[30]:
a <- rep(10, 5)
print(a)
[1] 10 10 10 10 10