Skip to content
Share
Explore

icon picker
R Operators

Operators are used to perform operations on variables and values.
1) Arithmetic operators are used with numeric values to perform common mathematical operations.
Arithmetic Operators
Operator
Name
Example
Result
+
Addition
20+3
23
-
Subtraction
20-3
17
*
Multiplication
20*3
60
/
Division
20/3
6.666667
^
Exponent
20^3
8000
%%
Modulus
20%%3
2
%/%
Integer Division (round down)
20%/%3
6
There are no rows in this table
2) Assignment operators are used to assign values to variables.
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->> my_var

## x <- 3 is equal to 3 -> x
## Note: <<- is a global assigner.

3) Comparison operators are used to compare two values.
Comparison Operators
Operator
Name
Example
==
Equal
x == y
!=
Not equal
x != y
>
Greater than
x > y
<
Less than
x < y
>=
Greater than or equal to
x >= y
<=
Less than or equal to
x <= y
There are no rows in this table
4) Logical operators are used to combine conditional statements.
Logical Operators
Operator
Description
&
Element-wise Logical AND operator. Returns TRUE if both elements are TRUE
&&
Logical AND operator - Returns TRUE if both statements are TRUE
|
Elementwise- Logical OR operator. Returns TRUE if one of the statements is TRUE
||
Logical OR operator. Returns TRUE if one of the statements is TRUE
!
Logical NOT - Returns FALSE if statement is TRUE
There are no rows in this table
x <- c(TRUE, FALSE, TRUE)
y <- c(FALSE, TRUE, TRUE)
x & y

## Output:
[1] FALSE FALSE TRUE
x <- 5
if (x > 0 && x < 10)
{
print("x is positive and less than 10")
}
else
{
print("x is either negative or greater than 10")
}

## Output:
[1] "x is positive and less than 10"

5) Miscellaneous operators are used to manipulate data
Miscellaneous Operators
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
There are no rows in this table

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.