Logical Vectors

Lecture 10

Dr. Eric Friedlander

College of Idaho
CSCI 2025 - Winter 2026

Introduction

Logical Vectors

  • A logical vector is a vector that contains only TRUE, FALSE, and NA (missing) values.
  • They are rarely in your raw data, but you will create and use them in almost every analysis.
  • Logical vectors are the foundation of filtering and conditional transformations.
  • In CS Logical Vectors are often called “Boolean” vectors.

Comparisons

Creating Logicals

  • You can create logical vectors with comparison operators:
    • > (greater than), < (less than)
    • >= (greater than or equal to), <= (less than or equal to)
    • == (equal to), != (not equal to)
x <- c(1, 2, 3, 5, 8, 13)
x > 4
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

Comparisons with filter()

  • Most often, you’ll use comparisons inside filter() to select rows.
  • Let’s use the flights dataset to find flights that were delayed on arrival.

Floating Point Comparison

  • Be careful when using == with numbers. Computers can have small rounding errors.
  • Use near() from the dply package to compare two numbers for “close enough” equality.
  • Let’s see an example.

Missing Values

NA

  • NA represents a missing value.
  • NA is “contagious”: almost any operation involving NA will produce NA.
  • You can’t use == to find missing values.
NA > 5
[1] NA
NA == 10
[1] NA
NA == NA
[1] NA

is.na()

  • Use the is.na() function to test if a value is missing.
x <- c(1, NA, 3)
is.na(x)
[1] FALSE  TRUE FALSE

Filtering and NA

  • filter() only includes rows where the condition is TRUE. It excludes FALSE and NA values.
  • If you want to include missing values in your filter, you must ask for them explicitly.

Removing Missing Values

  • Use drop_na to remove rows with missing values.
  • Specify the columns you want to remove from.
  • Avoid dropping more rows than necessary.

Practice!

Let’s do some practice!

Boolean Algebra

Combining Conditions

  • You can combine multiple logical conditions using boolean operators:
    • & (and)
    • | (or)
    • ! (not)

Examples

  • Let’s select flights that were delayed on both departure and arrival.
  • Let’s select flights that were delayed on either departure or arrival.
  • Let’s select flights that were not delayed on either departure or arrival.
  • Let’s select flights that were from November or December.

%in%

  • A useful shortcut for x == a | x == b | x == c is x %in% c(a, b, c).
  • It checks if each value in the left vector is present in the right vector.

Summarizing Logicals

Numeric Summaries

  • When you use a logical vector in a numeric context, TRUE becomes 1 and FALSE becomes 0.
  • sum(): counts the number of TRUEs.
  • mean(): calculates the proportion of TRUEs.

Practice!

  • Did Delta have any departure delays on Christmas Day? If so how many?
  • How many departure delays did AA, DL, and UA have on Christmas Day?
  • Plot the total number of departure delays per month. Per day.

Conditional Transformations

if_else()

  • if_else(condition, value_if_true, value_if_false)
  • All output values must be of the same type.
x <- c(-3, -1, 0, 1, 3)
if_else(x > 0, "positive", "negative or zero")
[1] "negative or zero" "negative or zero" "negative or zero" "positive"        
[5] "positive"        

case_when()

  • For multiple conditions, case_when() is easier to read than nested if_else().
  • case_when(condition1 ~ value1, condition2 ~ value2, ...)
  • The first TRUE condition wins.
Code
case_when(
  x < 0 ~ "negative",
  x == 0 ~ "zero",
  x > 0 ~ "positive"
)
[1] "negative" "negative" "zero"     "positive" "positive"

Example: case_when()

  • Let’s create a human-readable “season” variable.

Wrap Up

Logical Vectors

  • Create with comparisons: <, ==, %in%, is.na().
  • Combine with boolean operators: &, |, !.
  • Summarize with any(), all(), sum(), mean().
  • Use for conditional transformations with if_else() and case_when().
  • Logical vectors are a fundamental tool for data manipulation in R.

Do Next

  1. Read Chapter 12: Logicals from r4ds.
  2. Open the Recitation Gem and say “Provide me practice problems for Chapter 12” or work through some of the exercises in the text.
  3. Move on the Lecture 11!