0
0
R Programmingprogramming~10 mins

Logical indexing in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical indexing
Start with vector
Create logical condition
Apply condition to vector
Select elements where condition is TRUE
Return filtered vector
Logical indexing uses a TRUE/FALSE condition to pick elements from a vector.
Execution Sample
R Programming
x <- c(5, 10, 15, 20, 25)
cond <- x > 15
x[cond]
Selects elements from x that are greater than 15.
Execution Table
StepActionCondition/ExpressionResultOutput
1Create vector xc(5, 10, 15, 20, 25)x = [5, 10, 15, 20, 25]
2Evaluate conditionx > 15cond = [FALSE, FALSE, FALSE, TRUE, TRUE]
3Apply logical indexingx[cond]Select elements where cond is TRUE[20, 25]
4EndNo more steps
💡 All elements checked; only those with TRUE in cond are selected.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
xundefined[5, 10, 15, 20, 25][5, 10, 15, 20, 25][5, 10, 15, 20, 25][5, 10, 15, 20, 25]
condundefinedundefined[FALSE, FALSE, FALSE, TRUE, TRUE][FALSE, FALSE, FALSE, TRUE, TRUE][FALSE, FALSE, FALSE, TRUE, TRUE]
outputundefinedundefinedundefined[20, 25][20, 25]
Key Moments - 2 Insights
Why does x[cond] only return some elements, not all?
Because cond is a logical vector with TRUE only for elements greater than 15 (see execution_table step 2 and 3). Only those TRUE positions are selected.
What happens if cond has a different length than x?
In R, logical vectors used for indexing must be the same length as x or will recycle, which can cause unexpected results. Here cond matches x length exactly (execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of cond for the third element?
ANA
BTRUE
CFALSE
D15
💡 Hint
Check the 'Result' column in step 2 of execution_table; cond is [FALSE, FALSE, FALSE, TRUE, TRUE]
At which step does the program select elements from x using cond?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table; step 3 applies logical indexing.
If cond was all FALSE, what would be the output at step 3?
A[5, 10, 15, 20, 25]
B[] (empty vector)
C[TRUE, TRUE, TRUE, TRUE, TRUE]
DError
💡 Hint
Logical indexing returns elements where cond is TRUE; if none TRUE, output is empty (see variable_tracker output).
Concept Snapshot
Logical indexing in R:
- Create a logical vector condition (e.g., x > 15)
- Use it inside square brackets: x[condition]
- Returns elements where condition is TRUE
- Logical vector length should match x
- Useful for filtering vectors easily
Full Transcript
Logical indexing in R means using a TRUE/FALSE condition to pick elements from a vector. First, we create a vector x with values 5, 10, 15, 20, 25. Then we create a logical condition cond that checks which elements are greater than 15. This results in cond being FALSE for the first three elements and TRUE for the last two. When we use x[cond], R returns only the elements where cond is TRUE, which are 20 and 25. This process filters the vector based on the condition. It's important that the logical vector cond has the same length as x to avoid unexpected behavior. If cond had all FALSE values, the result would be an empty vector. Logical indexing is a simple and powerful way to select elements from vectors in R.