R — Operators
Arithmetic operators
a <- 10
b <- 3
a + b # 13 — addition
a - b # 7 — subtraction
a * b # 30 — multiplication
a / b # 3.333... — division
a %/% b # 3 — integer division
a %% b # 1 — modulo (remainder)
a ^ b # 1000 — exponentiation
R doesn't have // for integer division — it uses %/%. The %% operator gives the remainder.
Vectorized arithmetic
All arithmetic operators work element-wise on vectors:
x <- c(1, 2, 3, 4, 5)
y <- c(10, 20, 30, 40, 50)
x + y # 11 22 33 44 55
x * y # 10 40 90 160 250
x^2 # 1 4 9 16 25
# Recycling — shorter vector is repeated
x + 10 # 11 12 13 14 15
x * 2 # 2 4 6 8 10
When vectors have different lengths, R "recycles" the shorter one.
Comparison operators
a <- 10
b <- 20
a == b # FALSE
a != b # TRUE
a > b # FALSE
a < b # TRUE
a >= 10 # TRUE
a <= 5 # FALSE
Comparison operators return logical vectors:
x <- c(1, 5, 10, 15, 20)
x > 10 # FALSE FALSE FALSE TRUE TRUE
x == 5 # FALSE TRUE FALSE FALSE FALSE
Logical operators
a <- TRUE
b <- FALSE
a & b # FALSE — AND (element-wise)
a | b # TRUE — OR (element-wise)
!a # FALSE — NOT
# Short-circuit versions (for scalar logic)
a && b # FALSE — AND (first match only)
a || b # TRUE — OR (first match only)
& and | work element-wise on vectors. && and || evaluate only the first element — use them in if conditions.
# Vectorized — returns a vector
c(TRUE, FALSE, TRUE) & c(TRUE, TRUE, FALSE)
# TRUE FALSE FALSE
# Scalar — used in if statements
if (TRUE && FALSE) {
print("This won't run")
}
Assignment operators
x <- 10
x += 5 # NOT supported in R!
x = x + 5 # this is how you do it
# R doesn't have +=, -=, *=, etc.
R doesn't have compound assignment operators. Use x <- x + 5 instead.
Membership operators
x <- c(1, 2, 3, 4, 5)
5 %in% x # TRUE
6 %in% x # FALSE
c(1, 6) %in% x # TRUE FALSE
# For characters
fruits <- c("apple", "banana", "cherry")
"apple" %in% fruits # TRUE
"grape" %in% fruits # FALSE
%in% checks if a value exists in a vector. Essential for filtering and conditional logic.
Sequence operator
1:10 # 1 2 3 4 5 6 7 8 9 10
5:1 # 5 4 3 2 1
1.5:5.5 # 1.5 2.5 3.5 4.5 5.5
The : operator creates integer sequences. For more control, use seq().
Formula operator
# Used in statistical modeling
y ~ x # "y as a function of x"
y ~ x1 + x2 # "y as a function of x1 and x2"
The ~ operator defines relationships in formulas for lm(), glm(), and other modeling functions.
Pipe operators
library(dplyr)
# Basic pipe %>%
mtcars %>% head(5)
# Compound pipe %<>% (magrittr)
x %<>% sqrt() # same as x <- x %>% sqrt()
# Base R pipe |> (R 4.1+)
mtcars |> head(5)
Pipes chain operations together. |> is the native pipe (R 4.1+); %>% is from magrittr and has more features.
Operator precedence
From highest to lowest:
()— parentheses^— exponentiation-+— unary minus/plus:— sequence%%%/%— modulo, integer division*/— multiplication, division+-— addition, subtraction<><=>===!=— comparison!— logical NOT&&&— AND|||— OR~— formula-><-<<-— assignment
When in doubt, use parentheses.
Matrix operators
A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)
A + B # element-wise addition
A * B # element-wise multiplication
A %*% B # matrix multiplication
t(A) # transpose
solve(A) # inverse
det(A) # determinant
The %*% operator performs matrix multiplication — different from * which is element-wise.
Mini Practice
- Calculate the remainder of
17 %/% 5and17 %% 5 - Use
%in%to check if a word exists in a character vector - Create two vectors and perform element-wise operations (+, *, ^)
- Use the pipe operator to chain
mtcarsoperations - Create two 2x2 matrices and compute their matrix product with
%*%
Next: vectors — R's fundamental data structure →
Related Topics
Frequently Asked Questions about Operators
What is Operators in R?
Operators is a fundamental concept in R. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Operators?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.
Why is Operators important in R?
Operators is essential for R development. Understanding this concept will help you write better code and solve real-world problems more effectively.