Operators are used to perform operations on values and variables. The R operators are classified into six different categories:

- Arithmetic operators
- Comparison operators
- Logical operators
- Element-wise Logical operators
- Membership operators
- Assignment operators

## Arithmetic Operators

Arithmetic operators are used to perform simple mathematical operations on numeric values and vectors.

Operator | Meaning | Example |

+ | Addition | x + y |

– | Subtraction | x – y |

* | Multiplication | x * y |

/ | Division | x / y |

%% | Modulus | x %% y |

^ | Exponents | x ^ y |

%/% | Integer division | x %/% y |

Here are some examples:

```
# Operations on numeric values
x <- 6
y <- 2
# addition
x + y
[1] 8
# subtraction
x - y
[1] 4
# multiplication
x * y
[1] 12
# division
x / y
[1] 3
# modulus
x %% y
[1] 0
# exponents
x ^ y
[1] 36
# integer division
x %/% y
[1] 3
```

All the basic arithmetic operators can be performed on pairs of vectors. Each operation is performed in an element-by-element manner.

```
# Operations on vectors
v1 <- c(11,12,13,14,15)
v2 <- c(1,2,3,4,5)
# addition
v1 + v2
[1] 12 14 16 18 20
# subtraction
v1 - v2
[1] 10 10 10 10 10
# multiplication
v1 * v2
[1] 11 24 39 56 75
# division
v1 / v2
[1] 11.000000 6.000000 4.333333 3.500000 3.000000
# exponents
v1 ^ v2
[1] 11 144 2197 38416 759375
```

## Comparison Operators

Comparison operators are used to compare two values or vectors.

Operator | Meaning | Example |

== | Equal to | x == y |

!= | Not equal to | x != y |

> | Greater than | x > y |

< | Less than | x < y |

>= | Greater than or equal to | x >= y |

<= | Less than or equal to | x <= y |

Here are some examples:

```
# Operations on numeric values
x <- 6
y <- 2
# equal to
x == y
[1] FALSE
# not equal to
x != y
[1] TRUE
# greater than
x > y
[1] TRUE
# less than
x < y
[1] FALSE
# greater than or equal to
x >= y
[1] TRUE
# less than or equal to
x <= y
[1] FALSE
```

```
# Operations on vectors
v1 <- c(11,2,13,4,15)
v2 <- c(1,12,3,14,5)
# equal to
v1 == v2
[1] FALSE FALSE FALSE FALSE FALSE
# not equal to
v1 != v2
[1] TRUE TRUE TRUE TRUE TRUE
# greater than
v1 > v2
[1] TRUE FALSE TRUE FALSE TRUE
# less than
v1 < v2
[1] FALSE TRUE FALSE TRUE FALSE
# greater than or equal to
v1 >= v2
[1] TRUE FALSE TRUE FALSE TRUE
# less than or equal to
v1 <= v2
[1] FALSE TRUE FALSE TRUE FALSE
```

## Logical Operators

Logical operators are used to join two or more conditions.

Operator | Description | Example |

&& | Returns True if both statements are true | x > 0 && y < 0 |

|| | Returns True if one of the statements is true | x > 0 || y < 0 |

! | Reverses the result, returns False if the result is true | !(x > 0 && y < 0) |

```
x <- 2
y <- -2
# and
x > 0 && y < 0
[1] TRUE
# or
x > 0 || y < 0
[1] TRUE
# not
!(x > 0 && y < 0)
[1] FALSE
```

## Element-wise Logical Operators

These operators are used to perform logical operations on vectors in an element-by-element manner.

Operator | Description | Example |

& | Returns True if respective elements of both vectors are true | v1 && v2 |

| | Returns True if one of the respective elements of both vectors is true | v1 || v2 |

Here are some examples:

```
v1 <- c(0,1,2,3,4)
v2 <- c(0,1,0,1,0)
# and
v1 & v2
[1] FALSE TRUE FALSE TRUE FALSE
# or
v1 | v2
[1] FALSE TRUE TRUE TRUE TRUE
```

## Membership Operator

Membership operator is used to check if a specific item is present in the vector or the list.

Operator | Description | Example |

%in% | Returns True if a value is present in the vector or the list | x %in% y |

Here are some examples:

```
v <- list("red", "green", "blue")
"red" %in% v
[1] TRUE
"blue" %in% v
[1] TRUE
```

## Assignment Operators

Assignment operators are used to assign new values to variables.

Operator | Meaning | Example |

= | Assignment | x = 3 |

<-, <<-, = | Leftwards assignment | x <- 3, x <<- 3, x = 3 |

->, ->> | Rightwards assignment | 3 -> x, 3 ->> x |

Here are some examples:

```
x <- 3
x
[1] 3
x = 5
x
[1] 5
9 -> x
x
[1] 9
```

## Miscellaneous Operators

These operators are used to for special purposes.

Operator | Description | Example |

: | Generates a number sequence from a to b | 1:10 |

%*% | Multiplies two matrices | m1 %*% m2 |

Here are some examples:

```
# create a sequence
1:10
[1] 1 2 3 4 5 6 7 8 9 10
# multiplies two matrices
m1 <- matrix( c(1,2,3,4), nrow=2, ncol=2, byrow=TRUE)
m2 <- matrix( c(10,20,30,40), nrow=2, ncol=2, byrow=TRUE)
m1 %*% m2
[,1] [,2]
[1,] 70 100
[2,] 150 220
```

## Operator Precedence (Order of Operations)

In R, every operator is assigned precedence. Operator Precedence determines which operations are performed before which other operations.

Operators of highest precedence are performed first.

Operator | Description | |

highest precedence | ( { | Function calls and grouping expressions (respectively) |

[ [[ | Indexing | |

:: ::: | Access variables in a namespace | |

$ @ | Component / slot extraction | |

^ | Exponentiation (right to left) | |

– + | Unary minus and plus | |

: | Sequence operator | |

%any% | Special operators | |

* / | Multiply, divide | |

+ – | (Binary) add, subtract | |

< > <= >= == != | Ordering and comparison | |

! | Negation | |

& && | And | |

| || | Or | |

~ | As in formulas | |

-> ->> | Rightward assignment | |

= | Assignment (right to left) | |

<- <<- | Assignment (right to left) | |

lowest precedence | ? | Help (unary and binary) |