0
0
R Programmingprogramming~10 mins

Logical operators (&, |, !, &&, ||) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logical operators (&, |, !, &&, ||)
Start with two logical values
Apply & or && for AND
Apply | or || for OR
Apply ! for NOT
Get final TRUE or FALSE result
End
Logical operators combine or invert TRUE/FALSE values step-by-step to produce a final TRUE or FALSE.
Execution Sample
R Programming
a <- c(TRUE, FALSE, TRUE)
b <- c(FALSE, FALSE, TRUE)
result_and <- a & b
result_or <- a | b
result_not <- !a
This code applies element-wise AND, OR, and NOT logical operations on two logical vectors.
Execution Table
StepOperationOperandsResult
1a & ba=TRUE, FALSE, TRUE; b=FALSE, FALSE, TRUEFALSE, FALSE, TRUE
2a | ba=TRUE, FALSE, TRUE; b=FALSE, FALSE, TRUETRUE, FALSE, TRUE
3!aa=TRUE, FALSE, TRUEFALSE, TRUE, FALSE
4a && ba=TRUE, FALSE, TRUE; b=FALSE, FALSE, TRUEFALSE
5a || ba=TRUE, FALSE, TRUE; b=FALSE, FALSE, TRUETRUE
💡 All logical operations completed on vectors; && and || return single TRUE/FALSE from first elements.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
ac(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)
bc(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)
result_andNAc(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)c(FALSE, FALSE, TRUE)
result_orNANAc(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)c(TRUE, FALSE, TRUE)
result_notNANANAc(FALSE, TRUE, FALSE)c(FALSE, TRUE, FALSE)c(FALSE, TRUE, FALSE)
result_and_singleNANANANAFALSEFALSE
result_or_singleNANANANANATRUE
Key Moments - 3 Insights
Why does & return a vector but && returns a single TRUE or FALSE?
In row 1 and 4 of the execution_table, & works element-wise on all vector elements producing a vector result, while && only checks the first element of each vector and returns a single TRUE or FALSE.
What does the ! operator do to a logical vector?
As shown in row 3, ! flips each TRUE to FALSE and each FALSE to TRUE element-wise in the vector.
Why are the results of | and || different?
Row 2 shows | works element-wise returning a vector, while row 5 shows || returns a single TRUE or FALSE based on the first elements only.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of a & b at step 1?
ATRUE, FALSE, FALSE
BTRUE, TRUE, TRUE
CFALSE, FALSE, TRUE
DFALSE, TRUE, FALSE
💡 Hint
Check the 'Result' column in row 1 of the execution_table.
At which step does the operator return a single TRUE or FALSE instead of a vector?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Result' column for steps 4 and 5 in the execution_table.
If we change a to all FALSE, what would be the result of a || b at step 5?
AFALSE
BTRUE
CNA
DError
💡 Hint
|| returns TRUE only if the first element of a or b is TRUE; see variable_tracker for 'a' values.
Concept Snapshot
Logical operators in R:
& and | do element-wise AND/OR on vectors.
&& and || do single AND/OR on first elements only.
! negates (flips) TRUE/FALSE values.
Use & and | for vectors, && and || for single TRUE/FALSE checks.
Full Transcript
This visual execution shows how logical operators work in R. We start with two logical vectors a and b. Using & and | applies AND and OR to each element pair, producing vectors of TRUE or FALSE. The operators && and || only check the first element of each vector and return a single TRUE or FALSE. The ! operator flips TRUE to FALSE and vice versa for each element. The execution table traces each step's inputs and results, while the variable tracker shows how variables change. Key moments clarify why & differs from && and | differs from ||. The quiz tests understanding of these differences and results.