0
0
R Programmingprogramming~10 mins

Why control flow directs execution in R Programming - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a for loop.

R Programming
for (i in [1]) {
  print(i)
}
Drag options to blanks, or click blank then click option'
Aseq(5, 1)
Bc(1, 5)
C1:5
Drange(1, 5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(1, 5) which is not valid in R.
Using seq(5, 1) which counts backwards.
2fill in blank
medium

Complete the code to check if a number x is positive and print a message.

R Programming
if ([1] > 0) {
  print("Positive")
}
Drag options to blanks, or click blank then click option'
Ax
Bx != 0
Cx < 0
Dx == 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using x == 0 which checks for zero, not positive.
Using x < 0 which checks for negative numbers.
3fill in blank
hard

Fix the error in the code to print 'Even' for even numbers.

R Programming
if (x [1] 2 == 0) {
  print("Even")
}
Drag options to blanks, or click blank then click option'
Arem
Bmod
Cdiv
D%%
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mod' or 'rem' which are not valid operators in R.
Using 'div' which is not an operator in R.
4fill in blank
hard

Fill both blanks to create a vector of squares of numbers greater than 3.

R Programming
squares <- c()
for ([2] in 1:5) {
  if ([2] > 3) {
    squares <- c(squares, [1]^2)
  }
}
Drag options to blanks, or click blank then click option'
Ax
Bi
Cfor
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the two blanks.
Using 'for' which is a keyword, not a variable.
5fill in blank
hard

Fill all three blanks to create a named list of numbers greater than 2 with their squares.

R Programming
result <- list()
for ([3] in 1:5) {
  if ([3] > 2) {
    result[[paste0([1], [3] )]] <- [2]^2
  }
}
Drag options to blanks, or click blank then click option'
Anum
Bi
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the loop variable.
Using invalid names for the list element.