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 <-1010-> y
Arithmetic operators
Operator
Meaning
+
addition
-
subtraction
*
multiplication
/
division
^
exponentiation
%%
modulo
%/%
integer division
2+310%%310%/%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+214*(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&&FALSEFALSE&&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.
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 argument1:5|>mean()# base pipe with placeholder for a later argumentletters |>paste(collapse ="-"|> \(sep) paste("Letters:", ., sep = sep))# magrittr pipe often used with tidyversemtcars %>%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 -> matrixc(1, 2, 3) %o%c(10, 20)# Kronecker product: block matrix expansion of two matriceskronecker(A, B)