Tutorials References Exercises Videos Menu
Create Website Get Certified Upgrade

R Operators


Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

10 + 5
Try it Yourself »

R divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Miscellaneous operators

R Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator Name Example Try it
+ Addition x + y Try it »
- Subtraction x - y Try it »
* Multiplication x * y Try it »
/ Division x / y Try it »
^ Exponent x ^ y Try it »
%% Modulus (Remainder from division) x %% y Try it »
%/% Integer Division x%/%y Try it »

R Assignment Operators

Assignment operators are used to assign values to variables:

Example

my_var <- 3

my_var <<- 3

3 -> my_var

3 ->> my_var

my_var # print my_var
Try it Yourself »

Note: <<- is a global assigner. You will learn more about this in the Global Variable chapter.

It is also possible to turn the direction of the assignment operator.

x <- 3 is equal to 3 -> x



R Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example Try it
== Equal x == y Try it »
!= Not equal x != y Try it »
> Greater than x > y Try it »
< Less than x < y Try it »
>= Greater than or equal to x >= y Try it »
<= Less than or equal to x <= y Try it »

R Logical Operators

Logical operators are used to combine conditional statements:

Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is TRUE.
! Logical NOT - returns FALSE if statement is TRUE

R Miscellaneous Operators

Miscellaneous operators are used to manipulate data:

Operator Description Example
: Creates a series of numbers in a sequence x <- 1:10
%in% Find out if an element belongs to a vector x %in% y
%*% Matrix Multiplication x <- Matrix1 %*% Matrix2

Note: You will learn more about Matrix multiplication and matrices in a later chapter.