6.2 For and while loops
There are times when the apply()
function may not be the most suitable, for example when you are performing actions that do not return data, such as plotting data or running proof-of-concept workflows. This is where for
and while
loops are used. However, if you are using them for situations like the examples we saw above, they may be slower than using the apply
function.
To create a loop, we need to set a looping condition. There are two types in R:
a
for()
loop will run on each element of the input variable, anda
while()
loop repeats until an exit condition is reached. Note, you must ensure that you update the exit condition so it eventually becomes true, otherwise you will get an infinite looping error where you code never exists until it uses up all available resources (usually the memory).
We also need to wrap that loop in braces ({
and }
) to define the start and end of the code block to be iterated.
6.2.1 For loops
Below is a simple example of a for loop, which prints the column name of every fifth column in our temperature dataset.
years <- colnames(minTemp) # get the column names
for(year in years[seq(1,length(years), 5)]) {
print(year)
}
## [1] "X1949"
## [1] "X1954"
## [1] "X1959"
## [1] "X1964"
## [1] "X1969"
## [1] "X1974"
## [1] "X1979"
## [1] "X1984"
## [1] "X1989"
## [1] "X1994"
## [1] "X1999"
## [1] "X2004"
## [1] "X2009"
## [1] "X2014"
Something more complex, we loop through every 5th year in our temperature matrix and return the day with the lowest and highest temperature:
for (i in seq(1,ncol(minTemp),5)) {
year <- gsub("X", "", colnames(minTemp)[i])
temps <- minTemp[,i]
min.day <- which.min(temps)
max.day <- which.max(temps)
print(paste(rownames(minTemp)[min.day],"had the lowest min temp &",
rownames(minTemp)[max.day],"had the highest min temp in",year))
}
## [1] "Jul-20 had the lowest min temp & Dec-29 had the highest min temp in 1949"
## [1] "Jun-17 had the lowest min temp & Feb-1 had the highest min temp in 1954"
## [1] "Jun-27 had the lowest min temp & Jan-8 had the highest min temp in 1959"
## [1] "Aug-13 had the lowest min temp & Jan-14 had the highest min temp in 1964"
## [1] "Jul-23 had the lowest min temp & Feb-11 had the highest min temp in 1969"
## [1] "Jul-12 had the lowest min temp & Jan-8 had the highest min temp in 1974"
## [1] "Jul-2 had the lowest min temp & Dec-17 had the highest min temp in 1979"
## [1] "Jul-3 had the lowest min temp & Dec-16 had the highest min temp in 1984"
## [1] "Jul-22 had the lowest min temp & Mar-16 had the highest min temp in 1989"
## [1] "Aug-2 had the lowest min temp & Jan-9 had the highest min temp in 1994"
## [1] "Jun-18 had the lowest min temp & Jan-15 had the highest min temp in 1999"
## [1] "Jun-21 had the lowest min temp & Feb-22 had the highest min temp in 2004"
## [1] "Jun-12 had the lowest min temp & Jan-25 had the highest min temp in 2009"
## [1] "Jul-12 had the lowest min temp & Feb-20 had the highest min temp in 2014"
6.2.2 While loop
Below is a simple while loop that prints the Fibonacci series until the \(N^{th}\) number is divisible by 7:
#initialise the numbers
fib.prev <- 0
fib.curr <- 1
while (fib.curr %% 7 != 0) {
print(fib.curr)
# calculate new number
new.curr <- fib.curr + fib.prev
# update the pervious and current numbers
fib.prev <- fib.curr
fib.curr <- new.curr
}
## [1] 1
## [1] 1
## [1] 2
## [1] 3
## [1] 5
## [1] 8
## [1] 13
Going Loopy
-
R objects are often vectors and can be analysed in bulk using normal mathematical and statistical transformations. This applies for the vectorised data in
data.frames
andmatrices
. -
Use
for
andwhile
loops for testing ideas and exploring data, but try to avoid them if you can useapply
(or its variation) to get the same result in production code.
factors
cannot be used quite as easily in such analyses, so beware when operating on factors.