0
0
R Programmingprogramming~20 mins

Logical indexing in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Logical Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of logical indexing with NA values
What is the output of the following R code?
R Programming
x <- c(2, NA, 5, 8, NA)
result <- x[!is.na(x) & x > 4]
print(result)
A[1] 5 8
B[1] 2 5 8
C[1] NA NA
D[1] 5 8 NA
Attempts:
2 left
💡 Hint
Remember that logical indexing excludes NA when using !is.na().
Predict Output
intermediate
2:00remaining
Logical indexing with multiple conditions
What does this R code print?
R Programming
v <- c(10, 15, 20, 25, 30)
subset <- v[v %% 10 == 0 & v > 15]
print(subset)
A[1] 10 20 30
B[1] 20 30
C[1] 20
D[1] 15 25
Attempts:
2 left
💡 Hint
Check which numbers are multiples of 10 and greater than 15.
🔧 Debug
advanced
2:00remaining
Identify the error in logical indexing
What error does this R code produce?
R Programming
data <- c(1, 2, 3, 4, 5)
index <- c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)
result <- data[index]
print(result)
A[1] 1 2
BError: subscript out of bounds
C[1] 1 3 5
DError: logical subscript too long
Attempts:
2 left
💡 Hint
Check the length of the logical vector compared to the data vector.
Predict Output
advanced
2:00remaining
Logical indexing with named vectors
What is the output of this R code?
R Programming
scores <- c(John=85, Mary=92, Paul=78, Lisa=90)
high_scores <- scores[scores >= 90]
print(names(high_scores))
A[1] "Mary" "Lisa"
BNULL
C[1] "John" "Mary"
D[1] 92 90
Attempts:
2 left
💡 Hint
Logical indexing keeps names of selected elements.
Predict Output
expert
2:00remaining
Complex logical indexing with NA and recycling
What is the output of this R code?
R Programming
x <- c(NA, 3, NA, 7, 5)
idx <- c(TRUE, FALSE)
result <- x[!is.na(x) & idx]
print(result)
A[1] 3 7 5
B[1] 3 5
C[1] 5
DError: logical subscript too long
Attempts:
2 left
💡 Hint
Logical vector idx is recycled to match length of x.