Day 8 - Iteration

Spring 2023

Smith College

Overview

Timeline

  • A Refresher on Vectors
  • Iteration
  • For Loops
  • While

Goal

Understand the logic of iteration and how to use for loops

A Refresher on Vectors

An Ordered Sequence

In R, vectors are ordered meaning that the position of each element is recorded and important.


Even single values are a vector!

```{r}
# make example vector
num_vec = c(5, 10, 15, 20, 25, 30)

# Get the first element
num_vec[1]

# get the fifth element
num_vec[5]

# get the first and fourth element
num_vec[c(1, 4)]
```
[1] 5
[1] 25
[1]  5 20

Position, Name, or Condition

We can interact with and subset vectors in a number of ways:

Position
The location of the element(s) we want
Name
What the element(s) we want is called
Condition
A test which matches the element(s) we want
```{r}
named_vec = c("first" = 10, "second" = 22,
              "third" = 18, "forth" = 42,
              "fifth" = 100)

# subset by position
named_vec[2]

# subset by name
named_vec["third"]

# subset by condition
named_vec[named_vec > 20]
```
second 
    22 
third 
   18 
second  forth  fifth 
    22     42    100 

Iteration

What it means to Iterate

In R, iterating on something is working through a vector one element at a time.

Vector = c(2, 4, 6, 8, 10)

  • Iteration 1: c(2, 4, 6, 8, 10)
  • Iteration 2: c(2, 4, 6, 8, 10)
  • Iteration 3: c(2, 4, 6, 8, 10)
  • Iteration 4: c(2, 4, 6, 8, 10)
  • Iteration 5: c(2, 4, 6, 8, 10)

Multiple Data Objects

You can use iteration to apply the same process over multiple elements, like file paths.


You can tell R to read all of these files at once!

  • ./data/data_file_1.csv
  • ./data/data_file_2.csv
  • ./data/data_file_3.csv
  • ./data/data_file_4.csv
  • ./data/data_file_5.csv

When there is Dependence

Create a cumulative sum and tell me if that gets above 10.

Vector = c(1, 2, 1, 3, 2, 5, 3, 1, 4)

  • Iteration 1: c(1, 2, 1, 3, 2, 5, 3, 1, 4)
  • Iteration 2: c(3, 1, 3, 2, 5, 3, 1, 4)
  • Iteration 3: c(4, 3, 2, 5, 3, 1, 4)
  • Iteration 4: c(7, 2, 5, 3, 1, 4)
  • Iteration 5: c(9, 5, 3, 1, 4)
  • Iteration 6: c(14, 3, 1, 4)
  • STOP!

For Loops

For X in Y do Z



for(X in Y) {

    Do Z

}

For thing 1 in a sequence, do the following.


For thing 2 in sequence do the same.


For thing 3 in sequence do the same.


For thing 4 in sequence do the same.


For thing 5 in sequence do the same.

For number in number_vector


for(number in
c(3, 6, 9, 12, 15)) {

    Add 5

}

```{r}
# make the number vector
number_vector = c(3, 6, 9, 12, 15)

# iterate over vector
for(number in number_vector) {

  print(number + 5)

}
```
[1] 8
[1] 11
[1] 14
[1] 17
[1] 20

Notice that the results are not in a vector! For loops look at one element at a time!

Position or Content

Each THING

```{r}
for(letter in letters){
  
  print(letter)
  
}
```
[1] "a"
[1] "b"
[1] "c"
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
[1] "k"
[1] "l"
[1] "m"
[1] "n"
[1] "o"
[1] "p"
[1] "q"
[1] "r"
[1] "s"
[1] "t"
[1] "u"
[1] "v"
[1] "w"
[1] "x"
[1] "y"
[1] "z"

Each POSITION

```{r}
for(num in 1:26){
  
  print(letters[num])
  
}
```
[1] "a"
[1] "b"
[1] "c"
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
[1] "k"
[1] "l"
[1] "m"
[1] "n"
[1] "o"
[1] "p"
[1] "q"
[1] "r"
[1] "s"
[1] "t"
[1] "u"
[1] "v"
[1] "w"
[1] "x"
[1] "y"
[1] "z"

For Loops and Environments

For loops operate in whatever environment they are called in.


If called in the global environment, this means you can use any object in your global environment inside your for loop.


If inside a function, it will look for things inside the function first.

```{r}
# make a number vector
example_vector = c(1, 3, 5, 7, 9)

# need somewhere to store my results
results = c()

# multiply each thing in example_vector by 2
for(i in 1:length(example_vector)){
  
  results[i] = example_vector[i] * 2
  
}

print(i)
print(results)
```
[1] 5
[1]  2  6 10 14 18

While

While X is TRUE, do Y



while(X == TRUE) {

    Do Y

}

As long as X is TRUE, do Y.


Is X still TRUE? Do Y.


Is X still TRUE? Do Y.


Is X still TRUE? Do Y.


Is X still TRUE? Do Y.

While X is Smaller Do Y


while(i < 50) {

    Print i

}

```{r}
# make the number vector
iterator = 1

# iterate over vector
while(iterator < 50) {

  iterator = iterator + 10
  print(iterator)

}
```
[1] 11
[1] 21
[1] 31
[1] 41
[1] 51

Notice that the results are not in a vector! For loops look at one element at a time!

Caution with While

Be sure your condition will change, or it will go on forever!

```{r eval=FALSE}
# make the number vector
iterator = 1

# iterate over vector
while(iterator < 50) {

  iterator = iterator
  print(iterator)

}
```
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] 1
[1] "And so on..."

Special Loop Commands

Normal

```{r}
print("I'm counting!")

for(num in 1:10){
  print(paste0(
    num, " one thousand!"))
}
```
[1] "I'm counting!"
[1] "1 one thousand!"
[1] "2 one thousand!"
[1] "3 one thousand!"
[1] "4 one thousand!"
[1] "5 one thousand!"
[1] "6 one thousand!"
[1] "7 one thousand!"
[1] "8 one thousand!"
[1] "9 one thousand!"
[1] "10 one thousand!"

Break

```{r}
print("I'm counting!")

for(num in 1:10){
  
  if(num == 6){break}
  
  print(paste0(
    num, " one thousand!"))
}
```
[1] "I'm counting!"
[1] "1 one thousand!"
[1] "2 one thousand!"
[1] "3 one thousand!"
[1] "4 one thousand!"
[1] "5 one thousand!"

Next

```{r}
print("I'm counting!")

for(num in 1:10){
  
  if(num == 6){next}
  
  print(paste0(
    num, " one thousand!"))
}
```
[1] "I'm counting!"
[1] "1 one thousand!"
[1] "2 one thousand!"
[1] "3 one thousand!"
[1] "4 one thousand!"
[1] "5 one thousand!"
[1] "7 one thousand!"
[1] "8 one thousand!"
[1] "9 one thousand!"
[1] "10 one thousand!"

Code-Along

For Next Time

Topic

Lists and Apply

To-Do

  • Finish Worksheet