This lesson demonstrates how to analyze multiple data sets by sending them through a for loop.
Question 1
len <- 0
vowels <- c("a", "e", "i", "o", "u")
for (v in vowels) {
len <- len + 1
}
len
Upon completion of the preceding loop, the value of the variable len will be:
1. 0
2. 1
3. 6
4. 5
Question 2
letter <- "z"
for (letter in c("a", "b", "c")) {
print(letter)
}
Upon loop completion, the loop variable in the example above:
- will reset to “z”.
- will disappear.
- will be “c”.
- will return to “a”.
Analysis
- indicates a lack of understanding of program control and variable re-assignment.
- indicates a confusion between local and global variables, perhaps confusing loops with functions.
- correct answer.
- incorrect understanding of iteration over a loop variable.