Comparison Operators and Logical Operators

Materials adapted from Adrien Osakwe, Larisa M. Soto and Xiaoqi Xie.

Comparison operators are used to compare two values.

When you run a comparison, R evaluates the statement and returns a Logical value: either TRUE or FALSE.

Operator Name Example Plain English
== Equal to x == y Is x exactly equal to y?
!= Not equal to x != y Is x different from y?
> Greater than x > y Is x strictly larger than y?
< Less than x < y Is x strictly smaller than y?
>= Greater or equal x >= y Is x at least y or larger?
<= Less or equal x <= y Is x at most y or smaller?

Logical operators allow you to combine multiple comparison statements into a single test. This is essential for complex data filtering.

Operator Description
& Element-wise Logical AND operator. Returns TRUE if both elements are TRUE.
| Element-wise Logical OR operator. Returns TRUE if one of the statements is TRUE.
! Logical NOT. Returns FALSE if statement is TRUE; Reverses the result (TRUE becomes FALSE).
%in% Contains/Membership. Checks if a value belongs to a set.

1. Comparison operators

1.1 ==

In R, == is a logical operator for comparison. == asks: “Is A exactly equal to B?”

# Comparing numbers
10 == 2
[1] FALSE
# Comparing text (Case sensitive!)
"McGill" == "mcgill"
[1] FALSE
Warning

The Most Common R Error: = vs ==

= (or <-) is for Assignment: You are telling R that a variable equals a value.

== is for Comparison: You are asking R if two things are equal.

Incorrect Demonstrations

  1. Attempting to assign to a literal number

    # ERROR: This sounds like you are trying to force the number 10 
    # to take on the value 2. R will block this.
    10 = 2
    Error in `10 = 2`:
    ! invalid (do_set) left-hand side to assignment
  2. Confusing Variables with Text

    # ERROR: This creates a NEW variable named McGill and stores "mcgill" in it.
    # It does NOT check if they are the same.
    McGill = "mcgill"
    
    # To actually COMPARE them, you must use double equals and quotes for both:
    "McGill" == "mcgill"
    [1] FALSE

1.2 !=

In R, the exclamation mark ! stands for “NOT”. When combined with the equals sign, != checks if two values are different. != asks: “Is A different from B?”

  • It returns TRUE if the values are not the same (are different).
  • It returns FALSE if they are the same (are not different).
# Is 10 equal to 2? (False)
10 == 2
[1] FALSE
# Is 10 different from 2? (True)
10 != 2
[1] TRUE

2. Logical operators

2.1 &

The “AND” Operator (&): Use this when your condition must meet all criteria simultaneously.

age <- 65
bmi <- 32

# Is the patient over 60 AND obese (BMI > 30)?
age > 60 & bmi > 30
[1] TRUE

2.2 |

The “OR” Operator (|): Use this when meeting any one of the criteria is sufficient.

# Is the flower species either 'setosa' OR 'versicolor'?
species <- "setosa"
species == "setosa" | species == "versicolor"
[1] TRUE
Note

Keyboard Tip: How to type |?

Look for the key with the backslash \ (usually above the Return/Enter key).

  • Mac & Windows: Press Shift + \

2.3 !

The “NOT” Operator (!): This exclamation mark is used to reverse a logical value or exclude specific data points.

!TRUE
[1] FALSE
control_group <- FALSE
# Is the sample NOT in the control group?
!control_group
[1] TRUE

2.4 The Membership Operator (%in%)

The %in% operator (often called “contains”) is extremely useful when you want to check if a value belongs to a large list or group.

Example: Searching for Genes Imagine you have a list of “Genes of Interest” for your diabetes research. You can check if a specific gene is in that list without writing many OR statements.

diabetes_genes <- c("INS", "GCK", "KCNJ11", "SLC30A8")

# Is 'INS' (Insulin) in our list?
"INS" %in% diabetes_genes
## [1] TRUE

# Is 'SOX2' in our list?
"SOX2" %in% diabetes_genes
## [1] FALSE

3. Extended Reading: what is the difference between `&and&&`?

In R, you will sometimes see the double operator (&& and ||). While they look similar, they behave very differently under the hood:

Operator Type Behavior Best Use Case
& Vectorized Compares every element in a list. Data Filtering (e.g., filtering a whole column of 1,000 patients).
&& Control Flow Only looks at the first element and ignores the rest. if statements inside functions where you only expect one TRUE/FALSE.

In this workshop (and for 90% of data analysis), you should always use the single & and |. The double versions are advanced tools used mostly by package developers to make code run slightly faster in specific scenarios.