R Operators
Published Jan 9, 2022Updated May 29, 2025
Contribute to Docs
Operators are used in R to perform various operations on variables and values. Among the most commonly used ones are arithmetic and assignment operators.
Syntax
The following R code uses an arithmetic operator for multiplication, *, to calculate the product of two numbers, along with the assignment operator, <- to store the result in the variable x.
# Store five times three in variable xx <- 5 * 3
Operators in R can be organized into the following groups:
- Arithmetic operators for traditional mathematical evaluations such as addition and subtraction.
- Assignment operators for assigning values to variables.
- Comparison operators for testing equality between values.
- Logical operators for evaluating the “truthiness” of values against one another.
- Miscellaneous operators for various tasks including vectors and sequencing.
Arithmetic operators
R supports the following arithmetic operators:
- Addition,
+, which returns the sum of two numbers. - Subtraction,
-, which returns the difference between two numbers. - Multiplication,
*, which returns the product of two numbers. - Division,
/, which returns the quotient of two numbers. - Exponents,
^, which returns the value of one number raised to the power of another. - Modulus,
%%, which returns the remainder of one number divided by another. - Integer Division,
%/%, which returns the integer quotient of two numbers.
Assignment operators
R uses the following assignment operators:
<-assigns a value to a variable from right to left.->assigns a value to a variable left to right.<<-is a global version of<-.->>is a global version of->.=works the same way as<-, but its use is discouraged.
Comparison operators
R has the following comparison operators:
- Equal,
==, which returnsTRUEif two values are equal. - Not equal,
!=, which returnsTRUEif two values are not equal. - Less than,
<, which returnsTRUEif left value is less than right value. - Less than or equal to,
<=, which returnsTRUEif left value is less than or equal to right value. - Greater than,
>, which returnsTRUEif left value is greater than right value. - Greater than or equal to,
>=, which returnsTRUEif left value is greater than or equal to right value.
Logical operators
R has the following logical operators:
- Element-wise AND,
&, for comparing each element and returningTRUEif both elements areTRUE. - Logical AND,
&&, which returnsTRUEif both values areTRUE, only evaluates as many elements as necessary. - Element-wise OR,
|, for comparing each element and returningTRUEif either element isTRUE. - Logical OR,
||, which returnsTRUEif either value isTRUE, only evaluates as many elements as necessary. - Logical NOT,
!, which returnsTRUEif the associated statement isFALSE.
Note: The long form of AND and OR (&& and ||) are preferred for if statements as the short form can produce a vector value.
Miscellaneous operators
R uses the following miscellaneous operators:
- The
:operator creates a sequence of numbers from the left argument to the right one. - The
%in%operator returnsTRUEif the left argument is in the vector to the right. - The
%*%operator performs matrix multiplication on two matrices.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn R on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to code and clean and manipulate data for analysis and visualization with the R programming language.
- Beginner Friendly.14 hours