for Loops Answer Key

Author

Will Gammerdinger

Published

July 1, 2025

Exercise 1

  1. Create a for loop that goes from 1 to 8 and prints each number appended to the end of the text “Processing sample_” to the console.

Hint: One function we have not discussed previously but is helpful for this is paste0(), which pastes together strings.

# For each number in the vector 1 to 8, assign each to sample_number
for (sample_number in 1:8){
  # Print the sample_number appended to "Processing sample_" to the console
  print(paste0("Processing sample_", sample_number))
}
[1] "Processing sample_1"
[1] "Processing sample_2"
[1] "Processing sample_3"
[1] "Processing sample_4"
[1] "Processing sample_5"
[1] "Processing sample_6"
[1] "Processing sample_7"
[1] "Processing sample_8"