Bird
Raised Fist0
R Programmingprogramming~20 mins

Logical indexing in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does logical indexing in R do when applied to a vector like v[c(TRUE, FALSE, TRUE)]?
easy
A. Selects elements of v where the index is TRUE
B. Selects elements of v where the index is FALSE
C. Returns the length of v
D. Returns an error because logical vectors cannot index

Solution

  1. Step 1: Understand logical indexing

    Logical indexing uses a TRUE/FALSE vector to pick elements from the original vector where TRUE is present.
  2. Step 2: Apply logical vector to v

    Elements at positions with TRUE in the logical vector are selected; others are skipped.
  3. Final Answer:

    Selects elements of v where the index is TRUE -> Option A
  4. Quick Check:

    TRUE picks elements [OK]
Hint: TRUE means pick element, FALSE means skip [OK]
Common Mistakes:
  • Thinking FALSE picks elements
  • Confusing logical indexing with numeric indexing
  • Assuming logical vector length doesn't matter
2. Which of the following is the correct syntax to select elements of vector x where values are greater than 5 using logical indexing?
easy
A. x[> 5]
B. x > 5[x]
C. x[x > 5]
D. x[5 > x]

Solution

  1. Step 1: Understand logical condition syntax

    The condition x > 5 creates a logical vector TRUE where elements are greater than 5.
  2. Step 2: Use logical vector to index x

    Using x[x > 5] selects elements of x where the condition is TRUE.
  3. Final Answer:

    x[x > 5] -> Option C
  4. Quick Check:

    Correct logical indexing syntax [OK]
Hint: Put condition inside square brackets after vector [OK]
Common Mistakes:
  • Placing condition outside brackets
  • Using invalid syntax like x[> 5]
  • Swapping operands in condition
3. What is the output of the following R code?
v <- c(10, 3, 7, 2, 9)
v[v >= 7]
medium
A. [1] 10 3 7 2 9
B. [1] 3 2
C. Error: invalid logical indexing
D. [1] 10 7 9

Solution

  1. Step 1: Evaluate the logical condition v >= 7

    This creates a logical vector: TRUE, FALSE, TRUE, FALSE, TRUE for elements 10, 3, 7, 2, 9 respectively.
  2. Step 2: Select elements where condition is TRUE

    Elements at TRUE positions are 10, 7, and 9.
  3. Final Answer:

    [1] 10 7 9 -> Option D
  4. Quick Check:

    Only values >= 7 selected [OK]
Hint: Check which elements satisfy condition, pick those [OK]
Common Mistakes:
  • Including elements less than 7
  • Confusing >= with >
  • Expecting an error due to indexing
4. Identify the error in this R code snippet:
nums <- c(4, 8, 15, 16, 23, 42)
nums[nums > 10 & TRUE]
medium
A. No error, code runs correctly
B. Logical vector length mismatch
C. Incorrect use of & with TRUE
D. Syntax error due to missing parentheses

Solution

  1. Step 1: Analyze logical condition nums > 10 & TRUE

    The expression nums > 10 returns a logical vector; AND with TRUE keeps it unchanged.
  2. Step 2: Check for errors

    The logical vector length matches nums, and syntax is correct, so no error occurs.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Logical AND with TRUE keeps vector same [OK]
Hint: AND with TRUE keeps condition unchanged [OK]
Common Mistakes:
  • Thinking & TRUE causes error
  • Expecting length mismatch error
  • Missing that & works element-wise
5. Given a vector temps <- c(23, NA, 19, 25, NA, 21), how can you use logical indexing to select only the non-missing values?
hard
A. temps[is.na(temps)]
B. temps[!is.na(temps)]
C. temps[temps != NA]
D. temps[temps > NA]

Solution

  1. Step 1: Understand how to detect missing values

    The function is.na(temps) returns TRUE for missing values (NA) and FALSE otherwise.
  2. Step 2: Use logical NOT to select non-missing

    Using !is.na(temps) creates a logical vector TRUE where values are not missing.
  3. Step 3: Index temps with this logical vector

    This selects only the non-NA values from temps.
  4. Final Answer:

    temps[!is.na(temps)] -> Option B
  5. Quick Check:

    Use !is.na() to filter out NA [OK]
Hint: Use !is.na() to pick non-missing values [OK]
Common Mistakes:
  • Using temps != NA which is invalid
  • Selecting is.na(temps) to get missing values
  • Trying to compare with NA using > or <
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard