Bird
Raised Fist0
R Programmingprogramming~10 mins

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

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

Practice

(1/5)
1. Which logical operator in R checks every element of a vector and returns a vector of results?
easy
A. ==
B. & and |
C. !
D. && and ||

Solution

  1. Step 1: Understand vectorized logical operators

    In R, & and | operate element-wise on vectors, returning a vector of TRUE/FALSE values for each element.
  2. Step 2: Compare with single-element operators

    && and || only check the first element of the vectors and return a single TRUE/FALSE value.
  3. Final Answer:

    & and | -> Option B
  4. Quick Check:

    Element-wise operators = & and | [OK]
Hint: Use & and | for vectors, && and || for single values [OK]
Common Mistakes:
  • Confusing && and & as the same
  • Using || for vector comparisons
  • Thinking ! works on multiple elements like &
2. Which of the following is the correct syntax to negate a logical vector v in R?
easy
A. !v
B. not(v)
C. ~v
D. v != TRUE

Solution

  1. Step 1: Identify the negation operator in R

    The logical NOT operator in R is !, which negates each element of a logical vector.
  2. Step 2: Check other options

    not() is not a base R function, ~ is a formula operator, and v != TRUE works but is less direct and less clear.
  3. Final Answer:

    !v -> Option A
  4. Quick Check:

    Negate vector with ! [OK]
Hint: Use ! to flip TRUE/FALSE values in R [OK]
Common Mistakes:
  • Using not() which is not a base R function
  • Confusing ~ with negation
  • Using != TRUE instead of !
3. What is the output of the following R code?
v1 <- c(TRUE, FALSE, TRUE)
v2 <- c(FALSE, FALSE, TRUE)
v1 & v2
v1 && v2
medium
A. [1] TRUE TRUE TRUE [1] TRUE
B. [1] TRUE FALSE TRUE [1] TRUE
C. [1] FALSE FALSE FALSE [1] FALSE
D. [1] FALSE FALSE TRUE [1] FALSE

Solution

  1. Step 1: Evaluate element-wise AND with &

    v1 & v2 compares each element:
    TRUE & FALSE = FALSE
    FALSE & FALSE = FALSE
    TRUE & TRUE = TRUE
    Result: [FALSE FALSE TRUE]
  2. Step 2: Evaluate single-element AND with &&

    v1 && v2 checks only first elements:
    TRUE && FALSE = FALSE
    Result: [FALSE]
  3. Final Answer:

    [1] FALSE FALSE TRUE [1] FALSE -> Option D
  4. Quick Check:

    & compares all elements, && only first [OK]
Hint: & works element-wise; && checks first element only [OK]
Common Mistakes:
  • Thinking && works element-wise
  • Mixing up & and && results
  • Ignoring vector length in &&
4. Identify the error in this R code snippet:
v <- c(TRUE, FALSE, TRUE)
if (v & TRUE) {
  print("Yes")
} else {
  print("No")
}
medium
A. Using & inside if condition causes error due to vector length
B. Missing parentheses around v & TRUE
C. Should use | instead of &
D. No error, code runs fine

Solution

  1. Step 1: Understand if condition requirements

    The if statement expects a single TRUE or FALSE value, not a vector.
  2. Step 2: Analyze v & TRUE

    v & TRUE returns a logical vector of length 3, which causes an error in if because it cannot handle multiple values.
  3. Final Answer:

    Using & inside if condition causes error due to vector length -> Option A
  4. Quick Check:

    if needs single TRUE/FALSE, & returns vector [OK]
Hint: Use && in if conditions for single TRUE/FALSE [OK]
Common Mistakes:
  • Using & instead of && in if
  • Assuming if accepts vector
  • Ignoring error message about length
5. You have two logical vectors:
a <- c(TRUE, FALSE, TRUE, FALSE)
b <- c(FALSE, TRUE, TRUE, TRUE)

Write an expression using logical operators to find elements where either a is TRUE and b is FALSE, or a is FALSE and b is TRUE. What is the resulting vector?
hard
A. a | b resulting in [TRUE TRUE TRUE TRUE]
B. a & b resulting in [FALSE FALSE TRUE FALSE]
C. (a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE]
D. !(a | b) resulting in [FALSE FALSE FALSE FALSE]

Solution

  1. Step 1: Understand the condition

    We want TRUE where exactly one of a or b is TRUE, but not both. This is the XOR condition.
  2. Step 2: Write expression and evaluate

    (a & !b) | (!a & b) checks where a is TRUE and b is FALSE, or vice versa.
    Evaluate:
    a: TRUE FALSE TRUE FALSE
    b: FALSE TRUE TRUE TRUE
    a & !b: TRUE FALSE FALSE FALSE
    !a & b: FALSE TRUE FALSE TRUE
    OR: TRUE TRUE FALSE TRUE
  3. Final Answer:

    (a & !b) | (!a & b) resulting in [TRUE TRUE FALSE TRUE] -> Option C
  4. Quick Check:

    XOR = (a & !b) | (!a & b) [OK]
Hint: Use (a & !b) | (!a & b) for XOR logic [OK]
Common Mistakes:
  • Using & or | alone instead of XOR logic
  • Forgetting to negate one vector
  • Confusing XOR with 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