Chapter 16: R Booleans / Logical Values
R: Booleans / Logical Values.
These are the yes/no, true/false values that control almost everything in data analysis:
- Which rows to keep or remove
- Which calculations to perform
- Which points to color red in a plot
- Whether a condition is met
- Loop control, function behavior, error checking…
Let’s go through this slowly and thoroughly — like I’m sitting next to you in RStudio, showing every line live.
1. What Are Logical Values in R? (The Basics)
R has only two possible logical values:
- TRUE
- FALSE
They must be written in uppercase — true, True, tRuE will not work.
|
0 1 2 3 4 5 6 7 8 9 10 11 |
is_adult <- TRUE is_raining <- FALSE typeof(is_adult) # "logical" class(is_adult) # "logical" is.logical(is_raining) # TRUE |
Short aliases (very common in code):
- T = TRUE
- F = FALSE
|
0 1 2 3 4 5 6 7 |
passed <- T failed <- F |
→ Many experienced people use T/F in quick interactive work, but in scripts/packages write TRUE/FALSE for clarity and safety.
2. Where Do Logical Values Come From? (The Most Common Source)
99% of logical values are created by comparisons — not typed by hand.
Comparison operators:
| Meaning | Operator | Example | Result |
|---|---|---|---|
| Equal | == | 5 == 5 | TRUE |
| Not equal | != | Hyderabad != “Bengaluru” | TRUE |
| Greater than | > | temp > 30 | TRUE/FALSE |
| Greater or equal | >= | marks >= 40 | — |
| Less than | < | age < 18 | — |
| Less or equal | <= | score <= 100 | — |
Real Hyderabad examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
temp_now <- 29.5 is_hot <- temp_now > 30 is_very_hot <- temp_now >= 35 print(is_hot) # FALSE print(is_very_hot) # FALSE marks <- c(92, 68, 85, 45, 78) passed <- marks >= 40 print(passed) # TRUE FALSE TRUE TRUE TRUE |
3. Logical Operations – Combining Conditions (AND, OR, NOT)
These are the logical operators — they combine multiple TRUE/FALSE values.
| Meaning | Operator | Symbol | Example code | Result |
|---|---|---|---|---|
| AND | & | & | (temp > 28) & (humidity > 60) | TRUE if both true |
| OR | |
(marks < 40) |
||
| NOT | ! | ! | !is_raining | Opposite |
| AND (scalar) | && | && | if (x > 0 && y > 0) { ... } | Used in if() conditions (single value only) |
| OR (scalar) | || | || | if (error |
Important difference:
- & and → vectorized → work element-by-element on vectors
- && and || → single logical only → used inside if() / while()
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
temps <- c(28.5, 31.2, 29.8, 35.0, 27.1) humid <- c(65, 55, 72, 40, 80) # Vectorized AND / OR hot_and_humid <- (temps > 30) & (humid > 60) print(hot_and_humid) # FALSE FALSE TRUE TRUE FALSE either_hot_or_dry <- (temps > 30) | (humid < 50) print(either_hot_or_dry) # FALSE TRUE FALSE TRUE FALSE # NOT not_hot <- ! (temps > 30) print(not_hot) # TRUE FALSE TRUE FALSE TRUE |
4. Logical Values in Real Work – Filtering & Summaries
This is where they become super powerful.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Example data students <- data.frame( name = c("Aarav", "Priya", "Rahul", "Sneha", "Vikram"), marks = c(92, 68, 85, 45, 78), city = c("Hyd", "Bng", "Del", "Mum", "Hyd"), absent = c(FALSE, FALSE, TRUE, FALSE, FALSE) ) # Create logical vectors passed <- students$marks >= 50 from_hyd <- students$city == "Hyd" good_and_local <- passed & from_hyd # Use them to filter (most common use) students[passed, ] # only students who passed students[good_and_local, "name"] # names of Hyd students who passed # Count how many sum(passed) # 4 mean(passed) # 0.8 ← proportion who passed sum(!passed) # number who failed |
5. Missing Values (NA) & Logical Operations
NA is special — logical operations with NA usually give NA
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
scores <- c(85, NA, 92, 45, NA) passed <- scores >= 50 print(passed) # TRUE NA TRUE FALSE NA # How many definitely passed? sum(passed, na.rm = TRUE) # 2 # How many we know failed? sum(passed == FALSE, na.rm = TRUE) # 1 |
6. Quick Cheat-Sheet – Logical Essentials
- Values → TRUE, FALSE (or T, F)
- Comparisons → == != > >= < <=
- Combine → & (and), (or), ! (not)
- Vectorized → & and Single-value → && and || (inside if())
- Use in filtering → df[logical_vector, ]
- Count → sum(logical_vec), mean(logical_vec)
- Opposite → !logical_vec or logical_vec == FALSE
Your Mini Practice (Copy → Run Right Now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Hyderabad weather this week (mock data) days <- c("Mon", "Tue", "Wed", "Thu", "Fri") temps <- c(28.5, 31.2, 29.8, 35.0, 27.9) humid <- c(65, 55, 72, 40, 80) raining <- c(FALSE, TRUE, FALSE, FALSE, TRUE) # Create logical flags is_hot <- temps >= 30 is_humid <- humid >= 60 is_uncomfortable <- is_hot & is_humid has_rain <- raining # Report cat("Uncomfortable days:", sum(is_uncomfortable), "\n") cat("Days with rain or very hot:", sum(has_rain | is_hot), "\n") # Show which days days[is_uncomfortable] |
Output something like:
|
0 1 2 3 4 5 6 7 8 |
Uncomfortable days: 1 Days with rain or very hot: 3 [1] "Wed" |
Feel comfortable with logical values now?
Next questions?
- Want to practice complex filtering with multiple conditions?
- How logical values work inside dplyr (filter(), mutate(), if_else())?
- Or move to factors / vectors / subsetting?
Just tell me — whiteboard is ready! ☕🚀
