34  Boolean operation

Lahy sa vavy ny zanakao? Valiny: Eny

The title above is a joke between two logicians1. However, normal people never find it funny!

Operations with numbers are intuitive (Chapter 1), whereas operations on logical or Boolean variables (Chapter 33) are less intuitive. Consider the Boolean variables (also called statements or propositions) below:

A: Radama is male – TRUE

B: Radama is Malagasy – TRUE

C: Ranavalona is male – FALSE

D: Ranavalona is Malagasy – TRUE

Boolean operations combine propositions using logical operators:

P1: A AND B: Radama is male AND Radama is Malagasy.

P1 evaluates to TRUE. Formally: TRUE AND TRUE = TRUE.

P2: A OR B: Radama is male OR Radama is Malagasy.

P2 evaluates to TRUE. Formally: TRUE OR TRUE = TRUE.

P3: C AND D: Ranavalona is male AND Ranavalona is Malagasy.

P3 evaluates to FALSE. Formally: FALSE AND TRUE = FALSE.

P4: C OR D: Ranavalona is male OR Ranavalona is Malagasy.

P4 evaluates to TRUE. Formally: FALSE OR TRUE = TRUE.

Returning to the joke, the question can be reframed as two propositions:

Proposition 1: The newborn is male.

Proposition 2: The newborn is female.

Proposition 1 OR Proposition 2 evaluates to TRUE because one statement must be true.

Boolean operations form a fundamental part of programming and can feel unintuitive to non-logicians. Acquiring basic proficiency in logic is essential. Concrete applications are illustrated in the tips section below.

Formal

The AND operator is encoded as & in R (A & B or A && B).

The OR operator is encoded as | in R (A | B or A || B).

The single operators (&, |) perform element-wise evaluation, which serves as the primary method for vector filtering in data analysis.

The double operators (&&, ||) perform short-circuit evaluation, typically used in conditional statements and loops. These are rarely used in this handbook except in specific advanced scenarios.

A final symbol is !, which is used for negation. The expression !A evaluates to the logical opposite of A.

Examples

#| echo: true

# Assign Boolean values to variables
a <- TRUE
b <- TRUE
cc <- FALSE

# Logical OR: returns TRUE if at least one side is TRUE
a | b
[1] TRUE
# Logical AND: returns TRUE only if both sides are TRUE
a & b
[1] TRUE
# Logical AND with a FALSE value: evaluates to FALSE
a & cc
[1] FALSE
# Combined logical operation (AND has higher precedence than OR)
a | (b & cc)
[1] TRUE
# Grouped logical operation: parentheses force evaluation of (a | b) first
(a | b) & cc
[1] FALSE
# Negation: flips TRUE to FALSE (and vice versa)
!a
[1] FALSE

Tips and warnings

  • Although the concept of data frame is needed to full understand the example below. The example below combines and uses variable types, relational operators, and Boolean operators to filter individuals matching combined criteria:

    nationality == "Malagasy" & (male == TRUE | height < 1.6)

    In this expression, nationality, male, and height are variable names (Chapter 29) of type character (Chapter 32), logical (Chapter 33), and numeric (Chapter 30), respectively. The relational operators == and < filter within variables, whereas the Boolean operators & and | evaluate conditions across variables.

  • These Boolean operators correspond to the concepts of intersection (AND) and union (OR) in set theory. For example, in nationality == "Malagasy" & male == TRUE the operation essentialy intersect the set of malagasy and male.

  • Performing Boolean operations are prone to mistakes because logical evaluation can be surprisingly counterintuitive. Extra caution is required during implementation.


  1. mathematicians who specialize in the study of logic↩︎