Loops

Basic For Loops


1. The code below prints the numbers 1 through 5 one line at a time. Modify it to print the numbers 2 through 16.

for (i in 1:5){
  print(i)
}

2. 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.

for (i in 1:5){
  print(i)
}

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

birds = c('robin', 'woodpecker', 'blue jay', 'sparrow')
for (i in 1:length(_________)){
  print(birds[__])
}

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

radius <- c(1.3, 2.1, 3.5)
areas <- vector(_____ = "numeric", length = ______)
for (__ in 1:length(________)){
  areas[__] <- pi * radius[i] ^ 2
}
areas

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:

lengths = c(1.1, 2.2, 1.6)
widths = c(3.5, 2.4, 2.8)
areas <- vector(length = __________)
for (i in _____) {
  areas[__] <- lengths[__] * widths[__]
}
areas
[click here for output]