01 / 20
& |

Comparison &
Logical Operators

Master the building blocks of decision-making in R: comparison operators that test values and logical operators that combine conditions.

R Programming

What Are Comparison Operators?

Comparison operators compare two values and return a logical result: either TRUE or FALSE. They are the foundation of every if-statement, filter, and conditional expression you will write in R.

== and !=
Test if two values are equal or not equal
< and >
Test if one value is less than or greater than another
<= and >=
Include the boundary: less/greater than or equal to

Comparison Operators in Action

# Equality and inequality 5 == 5 # TRUE 5 != 3 # TRUE # Greater than / less than 10 > 7 # TRUE 4 < 2 # FALSE # With boundary (or equal to) 8 >= 8 # TRUE 3 <= 2 # FALSE

What Are Logical Operators?

Logical operators let you combine multiple conditions into a single expression. They take TRUE/FALSE inputs and produce a TRUE/FALSE output based on logical rules.

& and &&
AND: TRUE only when both conditions are TRUE
| and ||
OR: TRUE when at least one condition is TRUE
!
NOT: flips TRUE to FALSE and FALSE to TRUE

The Truth Table

A B A & B A | B !A TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE AND requires both TRUE : OR requires at least one TRUE : NOT inverts

Single vs Double: & vs &&

R has two versions of AND and OR. The key difference is whether they operate on entire vectors or just the first element.

Single (& and |) Double (&& and ||)
Operates on Entire vector (element-wise) First element only
Returns A vector of TRUE/FALSE A single TRUE or FALSE
Use in Filtering, vectorized ops if/else control flow
Short-circuit? No Yes

See the Difference in Code

# Vector AND (element-wise) x <- c(TRUE, FALSE, TRUE) y <- c(TRUE, TRUE, FALSE) x & y # TRUE FALSE FALSE # Scalar AND (first element only) x && y # TRUE

The NOT Operator: !

The ! operator flips a logical value. It turns TRUE into FALSE and FALSE into TRUE. It works on single values and entire vectors.

!TRUE # FALSE !FALSE # TRUE # Negate a comparison !(5 > 3) # FALSE (5 > 3 is TRUE, then flipped) # On a vector v <- c(TRUE, FALSE, TRUE) !v # FALSE TRUE FALSE

How to Apply These Operators

1
Identify the condition. What question are you asking? "Is x greater than 10?" translates to x > 10.
2
Pick the right comparison operator. Choose from ==, !=, <, >, <=, >= based on what you need to test.
3
Combine with logical operators if needed. Multiple conditions? Use & (AND), | (OR), or ! (NOT).
4
Choose single or double form. Working with a vector? Use & / |. Inside if()? Use && / ||.
5
Test and verify. Run the expression in R to confirm it returns the expected TRUE/FALSE values.

Example 1: Simple Comparisons

A student scored 85 on a test. Let's check various conditions about this score.

score <- 85 # Did they get a perfect score? score == 100 # FALSE # Did they pass (>= 60)? score >= 60 # TRUE # Is the score below 90? score < 90 # TRUE # Is the score NOT equal to 0? score != 0 # TRUE

Example 2: Combining Conditions

Check if a student's score qualifies for an honor roll: score at least 80 AND attendance above 90%.

score <- 85 attendance <- 95 # Both conditions must be TRUE score >= 80 && attendance > 90 # TRUE (85 >= 80 is TRUE, 95 > 90 is TRUE) # What if attendance drops? attendance <- 88 score >= 80 && attendance > 90 # FALSE (85 >= 80 is TRUE, 88 > 90 is FALSE)

Example 3: Vectorized Filtering

Filter a vector of temperatures to find days that were warm (above 75) or freezing (below 32).

temps <- c(28, 65, 80, 30, 92, 55) # Element-wise OR (use single |) warm_or_freeze <- temps > 75 | temps < 32 warm_or_freeze # TRUE FALSE TRUE TRUE TRUE FALSE # Use the logical vector to filter temps[warm_or_freeze] # 28 80 30 92

Short-Circuit Evaluation

The double operators && and || use short-circuit evaluation: R stops checking as soon as it knows the final answer.

&& (AND) If left side is FALSE... STOP: result is FALSE (right side never checked) || (OR) If left side is TRUE... STOP: result is TRUE (right side never checked) Why does this matter? Prevents errors: if (x != 0 && y/x > 2) When x is 0, the division is never attempted

Operator Precedence

When you combine multiple operators, R follows a specific order of operations. Highest priority runs first.

1
! (NOT) runs first: it binds tightest
2
<, >, <=, >=, ==, != (comparisons) run next
3
& (element-wise AND) runs after comparisons
4
| (element-wise OR) runs last among logicals

Common Mistakes to Avoid

✕ Using = Instead of ==
A single = is assignment, not comparison. Writing if (x = 5) assigns 5 to x instead of comparing.
✕ Wrong & in if()
Using & inside if() with vectors gives a warning. Use && for scalar conditions in control flow.
✕ Forgetting ! Precedence
Writing !x == 5 means (!x) == 5, not !(x == 5). Always wrap the expression: !(x == 5).

Complete Operator Reference

Operator Meaning Example Result
== Equal to 5 == 5 TRUE
!= Not equal 5 != 3 TRUE
< / > Less / Greater 3 < 7 TRUE
<= / >= Less/Greater or equal 5 >= 5 TRUE
& / && AND TRUE & FALSE FALSE
| / || OR TRUE | FALSE TRUE
! NOT !TRUE FALSE

Question 1 of 5

What does 10 != 10 return in R?

Question 2 of 5

What is the result of TRUE & FALSE?

Question 3 of 5

You have x <- c(3, 7, 2, 9). What does x > 5 return?

Question 4 of 5

Which operator should you use inside an if() statement to combine two conditions?

Question 5 of 5

What does !(5 > 3 | 2 == 2) evaluate to?