🤡Operators

Operators are the constructs that can manipulate the value of operands. Operators are used to performing operations on variables and values.

R divides the operators into the following groups:

  • Arithmetic operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Miscellaneous operators

Arithmetic Operators

Operator
Name
Example

+

Addition

x + y

-

Subtraction

x - y

*

Multiplication

x * y

/

Division

x / y

^

Exponent

x ^ y

%%

Modulus (Remainder from division)

x %% y

%/%

Integer Division

x%/%y

Assignment Operators

Assignment operators are used to assigning values to variables:

# Assign a variable
var1 <- 5

# Global variable
var2 <<- 8

# also same
5 -> var1

8 ->> var2

# Print the variables
var1
var2

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

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

x <- 3 is equal to 3 -> x

Comparison Operators

Comparison operators are used to comparing two values:

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

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 the statement is TRUE

NOTE: To understand the difference between & vs && and | vs ||click on the following link: R Basics 9 - Difference between single and double AND OR statements - & vs && and | vs ||

Miscellaneous Operators

Miscellaneous operators are used to manipulating 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

Sources of the contents on this page:

Last updated