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 uses to print numbers 1 through 5.
Complete the code to check if a number x is positive and print a message.
if ([1] > 0) { print("Positive") }
The condition x > 0 checks if x is positive. Here, x is the variable being compared.
Fix the error in the code to print 'Even' for even numbers.
if (x [1] 2 == 0) { print("Even") }
The modulo operator in R is %%, which gives the remainder of division. Using x %% 2 == 0 checks if x is even.
Fill both blanks to create a vector of squares of numbers greater than 3.
squares <- c() for ([2] in 1:5) { if ([2] > 3) { squares <- c(squares, [1]^2) } }
In R, to create a vector of squares for numbers greater than 3, you use a loop variable like i. Both blanks need the same variable name i.
Fill all three blanks to create a named list of numbers greater than 2 with their squares.
result <- list() for ([3] in 1:5) { if ([3] > 2) { result[[paste0([1], [3] )]] <- [2]^2 } }
The list is named with num, the loop variable is i, and the expression uses i to compute squares for numbers greater than 2.