In programming languages loop structures, either with or without conditions, are used to repeat commands over multiple entities. For and while loops as well as if-else statements are also often used in R, but perhaps not as often as in many other programming languages. The reason for this is that in R, there is an alternative called vectorization which usually is more efficient..
Vectorization implies that we can multiply all values in a vector in R by two by calling:
vec.a <- c(1, 2, 3, 4)
vec.a * 2
## [1] 2 4 6 8
In many other and languages as well as in R, you can also create this with a loop instead
for (i in vec.a) {
vec.a[i] <- vec.a[i] * 2
}
vec.a
## [1] 2 4 6 8
As you saw in the lecture, this is far less efficient and not by any means easier to type and we hence tend to avoid loops when possible.
for
-loop that calculates the sum for each row of the matrix.apply()
functionapply()
function rowSums()
function
X <- matrix(1:1000000, nrow = 100000, ncol = 10)
for.sum <- vector()
# Note that this loop is much faster if you outside the loop create an empty vector of the right size.
# rwmeans <- vector('integer', 100000)
for (i in 1:nrow(X)) {
for.sum[i] <- sum(X[i,])
}
head(for.sum)
## [1] 4500010 4500020 4500030 4500040 4500050 4500060
app.sum <- apply(X, MARGIN = 1, sum)
head(app.sum)
## [1] 4500010 4500020 4500030 4500040 4500050 4500060
rowSums.sum <- rowSums(X)
head(rowSums.sum)
## [1] 4500010 4500020 4500030 4500040 4500050 4500060
identical(for.sum, app.sum)
## [1] TRUE
identical(for.sum, rowSums.sum)
## [1] FALSE
identical(for.sum, as.integer(rowSums.sum))
## [1] TRUE
During the lecture an approach to calculate factorials was implemented using recursion (function calling itself). Here we should use recursion to generate a sequence of Fibonacci numbers. A Fibonacci number is part of a series of number with the following properties:
The first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. Hence:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
or
1, 1, 2, 3, 5, 8, 13, 21, ...
Write a function that generates Fibonacci number using a recursive approach.
fib_rec <- function(n) {
if (n == 0 || n == 1) {
return(n)
} else {
return(fib_rec(n - 1) + fib_rec(n - 2))
}
}
Generate Fibonacci numbers from 0 to 10 using *apply*
approach.
sapply(0:10, FUN = fib_rec)
## [1] 0 1 1 2 3 5 8 13 21 34 55
vec_fib_rec <- Vectorize(fib_rec)
vec_fib_rec(0:10)
## [1] 0 1 1 2 3 5 8 13 21 34 55
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] fontawesome_0.2.1 captioner_2.2.3 bookdown_0.22 knitr_1.33
##
## loaded via a namespace (and not attached):
## [1] compiler_4.0.3 magrittr_2.0.1 tools_4.0.3 htmltools_0.5.1.1
## [5] yaml_2.2.1 stringi_1.6.2 rmarkdown_2.8 stringr_1.4.0
## [9] xfun_0.23 digest_0.6.27 rlang_0.4.11 evaluate_0.14
Built on: 15-Jun-2021 at 15:30:23.
2021 • SciLifeLab • NBIS • RaukR