R Operators Cheat Sheet

Grouped overview with examples

Overview

This document lists the main operator groups in R, with tables and minimal examples.

Operators are symbols (and a few special names) that act on values or objects, e.g., +, <-, ==, %in%.


Assignment operators

Operator Meaning Notes
<- assign (preferred) standard in R (Alt+- in Rstudio)
-> assign (rightwards) rare
= assign in some contexts safe in function arguments
<<- assign in parent environment advanced
x <- 10
10 -> y

Arithmetic operators

Operator Meaning
+ addition
- subtraction
* multiplication
/ division
^ exponentiation
%% modulo
%/% integer division
2 + 3
10 %% 3
10 %/% 3
Tip

Tip: Use parentheses () to control the order of operations. By default, R follows standard mathematical precedence (PEMDAS/BODMAS). This means multiplication and division are done before addition and subtraction. Multiplication and division have the same precedence and are evaluated left to right.

14*10+2
14*(10+2)
(14*10)+2

Relational operators

Operator Meaning
< less than
<= less than or equal
> greater than
>= greater than or equal
== equal
!= not equal
x <- c(3, 5, 7)
x > 4

Logical operators

Element-wise

Operator Meaning
& AND
| OR
! NOT
(x > 4) & (x < 7)

Short-circuit

Short-circuit operators only evaluate the right side if needed, which is helpful to guard expensive or error-raising code.

Operator Meaning
&& AND
|| OR
TRUE && FALSE
FALSE && stop("This is never evaluated")
TRUE || stop("Also never evaluated")

Membership operator

Operator Meaning
%in% element in set
c("oak","pine") %in% c("oak","beech","pine")

Indexing operators

Operator Meaning
[ subset
[[ extract element
$ extract by name

In a matrix or data frame, use [,] to specify rows and columns respectively.

lst <- list(a=5:7,b=4)
lst$a
lst[[1]]
lst[[1]][2]
# matrix 2x4
mat <- matrix(1:8, nrow=2)
mat[1,3]  # row 1, column 3

Sequence operator

Operator Meaning
: integer sequence
1:5

Namespace operators

Operator Meaning
:: exported object
::: internal object
stats::median(1:5)

Formula operator

Operator Meaning
~ formula
lm(y ~ x, data=df)

Pipe operators

Pipes pass the left-hand value into the function on the right, helping you read code from left to right.

Operator Meaning
|> base pipe
%>% magrittr pipe
# base pipe: value becomes the first argument
1:5 |> mean()

# base pipe with placeholder for a later argument
letters |> paste(collapse = "-" |> \(sep) paste("Letters:", ., sep = sep))

# magrittr pipe often used with tidyverse
mtcars %>%
  subset(cyl == 4) %>%
  aggregate(mpg ~ gear, data = _, FUN = mean)

Special infix operators

Matrix-related infix operators combine vectors/matrices in different ways; dimensions matter.

Operator Meaning
%*% matrix multiplication
%o% outer product
%x% Kronecker product
A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(0, 1, 1, 0), nrow = 2)

# Matrix multiplication: (2x2) %*% (2x2) -> (2x2)
A %*% B

# Outer product: all pairwise products of two vectors -> matrix
c(1, 2, 3) %o% c(10, 20)

# Kronecker product: block matrix expansion of two matrices
kronecker(A, B)

Summary

Group Operators
Assignment <-, ->, =, <<-
Arithmetic +, -, *, /, ^, %%, %/%
Relational <, <=, >, >=, ==, !=
Logical &, |, !, &&, ||
Membership %in%
Indexing [, [[, $
Pipes |>, %>%