12  Loops in R, Part I

12.1 Acknowledgment/License

The original source for this chapter was from the web site

https://datacarpentry.org/semester-biology/

which was built using this underlying code

https://github.com/datacarpentry/semester-biology

and is used under the

Attribution 4.0 International (CC BY 4.0)

license https://creativecommons.org/licenses/by/4.0/.

The material presented here has been modified from the original source.

Accordingly this chapter is made available under the same license terms.

12.2 Source code

If you’d like to work within R Studio using the source code of this chapter, you can obtain it from here.

12.3 Basic for loop

  • Loops are the fundamental structure for repetition in programming
  • for loops perform the same action for each item in a list of things
for (item in list_of_items) {
  do_something(item)
}
  • To see an example of this let’s calculate masses from volumes using a loop
  • Need print() to display values inside a loop or function
  • Code in the loop will run once for each value in volumes
  • Everything between the curly brackets is executed each time through the loop
  • Code takes the first value from volumes and assigns it to volume and does the calculation and prints it
  • Then it takes the second value from volumes and assigns it to volume and does the calculation and prints it
  • And so on
  • So, this loop does the same exact thing as
Do Tasks 1 & 2 in Basic For Loops

1. The code below prints the numbers 1 through 5 one line at a time. Modify it to print each of these numbers multiplied by 3.

2. Write a for loop that loops over the following vector and prints out the mass in kilograms (mass_kg = 2.2 * mass_lb)

12.4 Looping with an index & storing results

  • R loops iterate over a series of values in a vector or other list like object
  • When we use that value directly this is called looping by value
  • But there is another way to loop, which is called looping by index
  • Looping by index loops over a list of integer index values, typically starting at 1
  • These integers are then used to access values in one or more vectors at the position inicated by the index
  • If we modified our previous loop to use an index it would look like this
  • We often use i to stand for “index” as the variable we update with each step through the loop
volumes = c(1.6, 3, 8)
for (i ...)
  • We then create a vector of position values starting at 1 (for the first value) and ending with the length of the object we are looping over
volumes = c(1.6, 3, 8)
for (i in 1:3)
  • We don’t want to have to know the length of the vector and it might change in the future, so we’ll look it up using the length() function
volumes = c(1.6, 3, 8)
for (i in 1:length(volumes)){

}
  • Then inside the loop instead of doing the calculation on the index (which is just a number between 1 and 3 in our case)
  • We use square brackets and the index to get the appropriate value out of our vector
  • This gives us the same result, but it’s more complicated to understand

  • So why would we loop by index?

  • The advantage to looping by index is that it lets us do more complicated things

  • One of the most common things we use this for are storing the results we calculated in the loop

  • To do this we start by creating an empty object the same length as the results will be before the loop starts

  • To store results in a vector we use the function vector to create an empty vector of the right length

  • mode is the type of data we are going to store

  • length is the length of the vector

  • Then add each result in the right position in this vector
  • For each trip through the loop put the output into the empty vector at the ith position
Do Tasks 3-4 in Basic For Loops.

3. Complete the code below so that it prints out the name of each bird one line at a time.

4. Complete the code below so that it stores one area for each radius.

12.5 Looping over multiple values

  • Looping with an index also allows us to access values from multiple vectors
Do Task 5 in Basic For Loops.

5. Complete the code below to calculate an area for each pair of lengths and widths, store the areas in a vector, and after they are all calculated print them out: