Basic Index (Loops)
The following code prints the first five whole numbers one line at a time:
for (i in 1:5){
print(i)
}
Modify this code to accomplish each of the following tasks:
- Multiply each of the numbers 1 through 5 by 3, then print the result.
- Use the vector
odds <- seq(1, 9, by = 2)
and a loop to square these numbers and print them one at a time. Choose a descriptive variable to identify the index values, such asfor (odd in odds)
. -
Instead of printing the values produced by this for loop, store them in a new vector named
output
. The following code provides a template for doing this:output <- c() for (odd in _____) { _____ <- _____^2 output <- c(output, odds_squared) }