0
0
R Programmingprogramming~10 mins

Logical indexing in R Programming - Interactive Code Practice

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

Complete 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'
A<
B!=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' selects elements less than 3, which is incorrect.
Using '==' selects elements equal to 3, not greater.
2fill in blank
medium

Complete 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'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' selects odd numbers, not even.
Using '>' or '<' does not correctly check for evenness.
3fill in blank
hard

Fix 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'
A!=
B>=
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' selects only elements equal to 4, not excluding them.
Using '>=' or '<=' does not exclude 4 properly.
4fill in blank
hard

Fill 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'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' changes the range to include boundary values.
Mixing up the operators reverses the logic.
5fill in blank
hard

Fill 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'
Aword
Bnchar(word)
Dlengths
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Not using nchar() returns wrong values.