Operators

==, !=, <, >, <=, >= — return logical vectors.

5.2 Comparison

==, !=, <, >, <=, >= — return logical vectors.

0.1 + 0.2 == 0.3                 # FALSE (floating point!)
isTRUE(all.equal(0.1 + 0.2, 0.3))  # TRUE — compare doubles with tolerance
identical(c(1, 2), c(1, 2))      # TRUE — exact object equality

5.3 Logical

element-wise AND / OR (vectorized) scalar AND / OR with short-circuit (use in if) any(), all()

Since R 4.3, &&/|| error if given vectors longer than 1.

c(TRUE, FALSE) & c(TRUE, TRUE)   # TRUE FALSE
TRUE || stop("never evaluated")  # TRUE (short-circuit)

5.4 Special operators

sequence 1:5 membership 3 %in% 1:5 native pipe (R 4.1+) magrittr pipe (tidyverse) list element / S4 slot access namespace access (exported / internal) user-defined infix operators

`%+%` <- function(a, b) paste(a, b)
"Hello" %+% "World"    # "Hello World"

5.5 Math functions

sqrt(16); abs(-3); exp(1); log(100); log10(100); log2(8); log(8, base = 2)
round(3.14159, 2); signif(123456, 2); ceiling(2.1); floor(2.9); trunc(-2.7)
factorial(5); choose(5, 2); sin(pi / 2); cos(0); max(3, 7); min(c(4, 1))
cumsum(1:5); cumprod(1:5); cummax(c(1, 3, 2)); diff(c(1, 4, 9))

5.6 Operator precedence (high → low)

^ → unary - + → : → %any% |> → * / → + - → comparison → ! → & && → | || → -> → <- → = → ?

-2^2     # -4  (^ binds tighter than unary minus)
1:3 * 2  # 2 4 6
1:(3*2)  # 1 2 3 4 5 6