Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

OR Operator in R: | vs ||, NA Handling and Subsetting

The OR operator in R: when to use | versus ||, how NA is handled, why || errors on vectors, and how to subset with OR.

Written and maintained by CASRAI Editorial Board

Last updated

The OR operator in R is written | (vectorised, element by element) or || (single value, left to right, stops as soon as the result is known). Use | when you filter or subset data and || inside if and while conditions. This guide is for researchers who run R for data cleaning and analysis and need the rules for both forms, including what happens with NA and with vectors longer than one element.

What the OR operator is used for in research code

Most research scripts use OR in two places: choosing rows or elements that meet at least one of several conditions (for example, keeping participants who are under 18 or over 65), and deciding whether a block of code should run (for example, stopping when a file is missing or a column is empty). The R documentation for the logical operators (?Logic) states that & and && indicate logical AND and | and || indicate logical OR. For background on R itself, see what R programming is used for.

Access R

R is distributed through the Comprehensive R Archive Network (CRAN) at cran.r-project.org. The operators on this page are part of base R, so no package needs to be installed. In any R session you can open the official help page with ?Logic or help("||").

Step by step: using | and ||

How does the || operator work?

|| evaluates its operands from left to right and proceeds only until the result is determined. The documentation says this longer form “is appropriate for programming control-flow and typically preferred in if clauses”. It returns a single logical value, and using vectors of more than one element in && or || gives an error.

  1. Write the two conditions that each produce one TRUE or FALSE: if (is.null(x) || length(x) == 0) { ... }
  2. If the left side is TRUE, R does not need the right side, because an OR with one TRUE operand is already TRUE. If the left side is FALSE, R evaluates the right side.
  3. Use the result as the if or while condition.

What is the difference between | and || in R?

The documentation states that the shorter forms (|, &) perform elementwise comparisons in much the same way as arithmetic operators. The R Language Definition’s operator table lists | as “Or, binary, vectorized” and || as “Or, binary, not vectorized”.

Situation Use Why (per the R documentation)
Subsetting a vector or filtering rows of a data frame | Compares element by element and returns one result per element
Condition of if or while || Returns one value, stops once the result is determined, typically preferred in if clauses
Operand has more than one element | only || with a vector of more than one element gives an error

Is it & or && in R?

Both exist, and they follow the same split as OR. & is the vectorised AND and && is the single-value, short-circuit AND. Choose by the same rule: & for subsetting, && for if conditions.

Subsetting with |

Because | returns one logical value per element, its result can index a vector directly:

x <- c(1, 3, 5, 6)
x < 2 | x > 4        # TRUE FALSE TRUE TRUE
x[x < 2 | x > 4]      # 1 5 6

The same pattern works for a data frame: df[df$age < 18 | df$age > 65, ] keeps the rows where either condition holds. Each comparison must be wrapped as its own expression; write x < 2 | x > 4, not x < 2 | 4, because the right-hand operand of | is evaluated on its own. For testing against several allowed values, x %in% c(1, 5, 6) is a common alternative to chaining several == comparisons with |.

What is ||=?

R has no ||= operator. No such operator is listed in the ?Logic help page or in the R Language Definition. To get the effect “keep x if it is TRUE, otherwise use y”, assign the result explicitly: x <- x || y. That works only when x and y are single logical values; for vectors use x <- x | y.

Output and how to read it

NA handling

NA is a valid logical value in R. The documentation says that where a component is NA, the result is NA if the outcome is ambiguous: NA & TRUE is NA, but NA & FALSE is FALSE. The Language Definition gives the OR counterpart: TRUE | NA is TRUE. Applying the same rule, the OR results are:

Expression Result Reason
TRUE | NA TRUE One TRUE operand decides the OR
FALSE | NA NA Outcome depends on the unknown value
NA | NA NA Both unknown
TRUE || NA TRUE Left side decides; the right side is not needed
FALSE || NA NA Right side is evaluated and is unknown

A condition that evaluates to NA cannot be used in if. The documentation notes that if(isTRUE(cond)) may be preferable to if(cond) because of NAs, since isTRUE(x) is the same as is.logical(x) && length(x) == 1 && !is.na(x) && x.

Result length

For |, if one operand is a zero-length vector the result has length zero; otherwise the elements of the shorter vector are recycled, with a warning when they are recycled only fractionally. For ||, the value is a length-one logical vector.

Troubleshooting

Error when a condition has more than one element

If c(TRUE, FALSE) || TRUE or an if condition built with || receives a vector longer than one, R signals an error, as the documentation states. Fix it by collapsing the vector to a single value first, or by switching to | if you meant an elementwise test:

  • any(x) and all(x): the documentation’s See Also entry for applying OR and AND across many scalar arguments.
  • isTRUE(x): a safe single-value test that is false for NA and for vectors longer than one.
  • xor(x, y): the elementwise exclusive OR, true when exactly one operand is true.

Numbers as conditions

Numeric and complex vectors are coerced to logical, with zero treated as false and all non-zero values as true. A bare number on one side of | therefore does not mean “equals that number”. Write the comparison out.

Other languages

Operators such as ||= that exist in other languages are not R syntax. R’s | and || are logical operators; they do not combine strings or merge data frames.

Related base R operators and functions

Need R form Source
Elementwise OR x | y ?Logic
Single-value OR for if x || y ?Logic
Elementwise AND / single-value AND x & y / x && y ?Logic
Negation !x ?Logic
Exclusive OR xor(x, y) ?Logic
OR across many scalar arguments any(...) ?Logic, See Also

For conditional logic that uses these operators on whole columns, see ifelse in R and case_when in R. For the objects you filter, see data frames in R, and for writing reusable conditions, functions in R.

FAQ

How does the || operator work?

It evaluates left to right, stops once the result is determined, and returns a single logical value. Vectors longer than one element produce an error.

What is ||=?

It is not an R operator. Write x <- x || y instead.

What is the difference between | and || in R?

| is vectorised and works element by element; || is not vectorised and is intended for single-value control-flow conditions.

Is it & or && in R?

Both. & is vectorised AND for subsetting; && is single-value AND for if conditions.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about OR Operator in R: | vs ||, NA Handling and Subsetting

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →