Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select elements greater than 3 from the vector.
R Programming
vec <- c(1, 4, 3, 5, 2) result <- vec[vec [1] 3] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' selects elements less than 3, which is incorrect.
Using '==' selects elements equal to 3, not greater.
✗ Incorrect
Using vec > 3 creates a logical vector selecting elements greater than 3.
2fill in blank
mediumComplete the code to select elements that are even numbers.
R Programming
vec <- c(2, 5, 8, 7, 10) even_nums <- vec[vec %% 2 [1] 0] print(even_nums)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' selects odd numbers, not even.
Using '>' or '<' does not correctly check for evenness.
✗ Incorrect
Using vec %% 2 == 0 selects elements divisible by 2, i.e., even numbers.
3fill in blank
hardFix the error in the code to select elements not equal to 4.
R Programming
vec <- c(4, 6, 4, 8, 5) result <- vec[vec [1] 4] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' selects only elements equal to 4, not excluding them.
Using '>=' or '<=' does not exclude 4 properly.
✗ Incorrect
Using vec != 4 selects elements that are not equal to 4.
4fill in blank
hardFill both blanks to select elements less than 5 and greater than 2.
R Programming
vec <- c(1, 3, 4, 6, 2) result <- vec[vec [1] 5 & vec [2] 2] print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' changes the range to include boundary values.
Mixing up the operators reverses the logic.
✗ Incorrect
Use < 5 and > 2 to select elements between 2 and 5.
5fill in blank
hardFill all three blanks to create a named vector with lengths of words longer than 3 characters.
R Programming
words <- c("cat", "house", "dog", "elephant") lengths <- c([1] = [2] for [3] in words if nchar([3]) > 3) print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Not using
nchar() returns wrong values.✗ Incorrect
Use word as the name, nchar(word) as the value, and iterate over word in words.