Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Logical operators are the decision-making engine of your R code. Every time you filter a dataset, write a conditional statement, or subset rows based on criteria, you're relying on these operators to evaluate what's TRUE and what's FALSE. You're being tested on your ability to construct conditions, combine multiple criteria, and predict the output of logical expressionsโskills that show up constantly in data manipulation and control flow questions.
Think of logical operators as falling into three categories: comparison operators (checking relationships between values), Boolean operators (combining conditions), and membership operators (checking if values belong to sets). Don't just memorize the symbolsโknow when to use & versus |, understand why == isn't the same as =, and recognize how these operators behave with vectors. That conceptual understanding is what separates students who can debug code from those who get stuck.
These operators compare two values and return a logical result. They work element-wise on vectors, meaning R compares each element in position and returns a vector of TRUE/FALSE values.
TRUE only when both sides match perfectlyc(1, 2, 3) == 2 returns FALSE TRUE FALSE, not a single valueTRUE when values differโthe logical opposite of ==df[df$category != "exclude", ] to remove unwanted rowsTRUE only when the left value exceeds the right5 > 5 returns FALSEif statements and while loops for threshold-based logicTRUE when the left value is smallerCompare: > vs. >=โboth test magnitude, but >= includes the boundary value while > excludes it. If an exam question asks about "at least 5" or "5 or more," you need >=, not >.
TRUE when the left value is greater than or exactly equal to the right(x > y) | (x == y) but more efficient and readableTRUE when the left value is smaller than or equal to the rightwhile(i <= n) to include the final iteration>= when defining inclusive ranges like x >= 5 & x <= 10Compare: == vs. =โthis is a classic error. == tests equality and returns TRUE/FALSE, while = is an assignment operator. Writing if(x = 5) will assign 5 to x, not test whether x equals 5.
These operators take logical values as input and return logical values as output. They let you build complex conditions from simpler ones, which is essential for multi-criteria filtering.
TRUEโreturns FALSE if either side is FALSEc(TRUE, FALSE) & c(TRUE, TRUE) returns TRUE FALSETRUEโonly returns FALSE when both sides are FALSECompare: & vs. |โAND narrows your results (both conditions must pass), while OR broadens them (either condition can pass). When filtering data, & typically returns fewer rows than |. Remember: & is restrictive, | is permissive.
TRUE becomes FALSE and vice versa!is.na(x) to find non-missing valuesCompare: & and | vs. && and ||โthe single versions (&, |) are vectorized and compare element-by-element. The double versions (&&, ||) only evaluate the first element and use short-circuit evaluation. Use single for data filtering, double for control flow in if statements.
This operator tests whether values belong to a defined set, which is more readable and efficient than chaining multiple == comparisons with |.
TRUE if the left value appears anywhere in the right vectorc(1, 2, 5) %in% c(1, 2, 3) returns TRUE TRUE FALSEx %in% c("a", "b", "c") replaces x == "a" | x == "b" | x == "c"Compare: %in% vs. chained == with |โboth can test multiple values, but %in% is more readable and less error-prone. For filtering rows where a column matches any of several values, always prefer %in%.
| Concept | Best Examples |
|---|---|
| Equality testing | ==, != |
| Magnitude comparison (strict) | >, < |
| Magnitude comparison (inclusive) | >=, <= |
| Combining conditions (restrictive) | & |
| Combining conditions (permissive) | ` |
| Negating conditions | ! |
| Set membership | %in% |
| Common errors to avoid | = vs. ==, & vs. && |
What is the difference between & and && in R, and when would you use each one?
Given x <- c(3, 7, 2, 9), what does x > 5 & x < 10 return, and why?
Compare and contrast x == 5 | x == 8 | x == 12 with x %in% c(5, 8, 12)โwhen would you prefer one over the other?
If you want to filter a dataframe to include rows where age >= 21 AND status != "inactive", write the logical expression you would use inside the subset brackets.
What does !(x > 5) return when x <- c(3, 5, 7), and how does this differ from x <= 5?