Logical indexing is a way to select elements from a vector or other data structure using a logical vector of TRUE and FALSE values. Only elements corresponding to TRUE are selected.
Click to reveal answer
beginner
How does logical indexing differ from numeric indexing in R?
Numeric indexing uses numbers to select elements by position, while logical indexing uses TRUE/FALSE values to select elements based on a condition.
Click to reveal answer
beginner
Example: What does x[c(TRUE, FALSE, TRUE)] return if x = c(10, 20, 30)?
It returns c(10, 30) because the first and third positions are TRUE, so those elements are selected.
Click to reveal answer
intermediate
How can logical indexing be used with conditions?
You can create a logical vector by testing a condition on elements, like x > 5, and use it to select only elements that meet the condition.
Click to reveal answer
intermediate
What happens if the logical vector is shorter than the vector being indexed?
The logical vector is recycled (repeated) to match the length of the vector being indexed.
Click to reveal answer
What does logical indexing select in R?
AElements at numeric positions
BElements where the logical vector is FALSE
CElements where the logical vector is TRUE
DAll elements
✗ Incorrect
Logical indexing selects elements corresponding to TRUE values in the logical vector.
If x = c(5, 10, 15), what does x[x > 7] return?
Ac(10, 15)
Bc(TRUE, TRUE, TRUE)
Cc(5)
Dc(5, 10, 15)
✗ Incorrect
x > 7 creates a logical vector c(FALSE, TRUE, TRUE), so x[x > 7] returns elements 10 and 15.
What happens if the logical vector is shorter than the vector being indexed?
AThe logical vector is recycled to match the length
BAn error occurs
COnly the first elements are selected
DNo elements are selected
✗ Incorrect
R recycles the shorter logical vector to match the length of the vector being indexed.
Which of these is a valid logical index for x = c(1, 2, 3, 4)?
Ac(1, 3)
Bc(TRUE, FALSE, TRUE, FALSE)
Cc('a', 'b')
Dc(0, 1, 0, 1)
✗ Incorrect
Logical indexing requires a logical vector of TRUE/FALSE values.
How can you select all elements of x that are equal to 10 using logical indexing?
Ax[c(10)]
Bx[10]
Cx == 10
Dx[x == 10]
✗ Incorrect
x == 10 creates a logical vector; using it inside x[...] selects elements equal to 10.
Explain how logical indexing works in R and give a simple example.
Think about how TRUE and FALSE values pick elements from a vector.
You got /3 concepts.
Describe what happens when the logical vector used for indexing is shorter than the vector being indexed.
Consider how R handles vectors of different lengths.
You got /3 concepts.
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
Step 1: Understand logical indexing
Logical indexing uses a TRUE/FALSE vector to pick elements from the original vector where TRUE is present.
Step 2: Apply logical vector to v
Elements at positions with TRUE in the logical vector are selected; others are skipped.
Final Answer:
Selects elements of v where the index is TRUE -> Option A
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
Step 1: Understand logical condition syntax
The condition x > 5 creates a logical vector TRUE where elements are greater than 5.
Step 2: Use logical vector to index x
Using x[x > 5] selects elements of x where the condition is TRUE.
Final Answer:
x[x > 5] -> Option C
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
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.
Step 2: Select elements where condition is TRUE
Elements at TRUE positions are 10, 7, and 9.
Final Answer:
[1] 10 7 9 -> Option D
Quick Check:
Only values >= 7 selected [OK]
Hint: Check which elements satisfy condition, pick those [OK]