Complete the code to print numbers from 1 to 5 using a for loop.
for (i in [1]) { print(i) }
The expression 1:5 creates a sequence from 1 to 5 in R, which the for loop iterates over.
Complete the code to calculate the sum of numbers from 1 to 10 using a for loop.
total <- 0 for (num in 1:10) { total <- total + [1] } print(total)
Inside the loop, we add the current number num to the total sum.
Fix the error in the for loop that prints each letter in the vector letters.
letters <- c('a', 'b', 'c') for ([1] in letters) { print(letter) }
The loop variable should be letter to match the variable used inside the loop.
Fill both blanks to create a named vector with squares of numbers greater than 3.
nums <- 1:5 squares <- c() for ([1] in nums) { if ([2] > 3) { squares[as.character(num)] <- num^2 } }
The loop variable is i, and the condition checks if num is greater than 3.
Fill all three blanks to create a list of even numbers and their squares from 1 to 6.
evens <- list() for ([1] in 1:6) { if ([2] %% 2 == 0) { evens[[as.character([3])]] <- [2]^2 } }
The loop variable is i, used consistently in the condition and as the list key.