## Arithmetic operators

The R arithmetic operators allows us to do **math operations**, like sums, divisions or multiplications, among others. The following table summarizes all the R base arithmetic operators.

Arithmetic operator in R | Description |
---|---|

+ | Plus |

– | Minus |

* | Multiplication |

/ | Division |

^ | Exponential |

** | Exponential |

%% | Modulus |

%/% | Integer divide |

%*% | Matrix multiplication |

%o% | Outer product |

%x% | Kronecker product |

In the next block of code you will find examples of basic calculations with arithmetic operations with integers.

```
#-----------------
# Basic operations
#-----------------
3 + 5 # 8
8 - 3 # 5
7 * 5 # 35
1/2 # 0.5
4 ^ 4 # 256
4 ** 4 # 256
5 %% 3 # 3
5 %/% 3 # 1
```

You can also use the basic operations **with R vectors of the same length**. Note that the result of these operations will be a vector with element-wise operation results.

```
#---------
# Vectors
#---------
x <- c(1, 7, 3)
y <- c(9, 4, 5)
x + y # 10 11 8
x - y # -8 3 -2
x * y # 9 28 15
x / 2 # 0.5 3.5 1.5
# Each element of the first vector elevated
# to the corresponding element of the second
x ** y # 1 2401 243
x ^ y # 1 2401 243
x %% y # 1 3 3
x %/% y # 0 1 0
```

Furthermore, you can use those arithmetic operators with matrix objects, besides the ones designed for this type of object (matrix multiplication types).

```
#---------
# Matrix
#---------
s <- matrix(1:6, nrow = 2, ncol = 3)
t <- matrix(8:13, nrow = 2, ncol = 3)
s
# [, 1] [, 2] [, 3]
# [1, ] 1 3 5
# [2, ] 2 4 6
t
# [, 1] [, 2] [, 3]
# [1, ] 8 10 12
# [2, ] 9 11 13
# Matrix sum
s + t
# [, 1] [, 2] [, 3]
# [1, ] 9 13 17
# [2, ] 11 15 19
# Multiplication element by element
s * t
# [, 1] [, 2] [, 3]
# [1, ] 8 30 60
# [2, ] 18 44 78
# Note you need the correct dimensions for matrix multiplication
w <- matrix(8:13, nrow = 2, ncol = 3)
z <- matrix(1:6, nrow = 3, ncol = 2)
w %*% z
# [, 1] [, 2]
# [1, ] 64 154
# [2, ] 70 169
# Outer multiplication
w %o% z # (Output ommited)
# Kronecker product
w %x% z
# [, 1] [, 2] [, 3] [, 4] [, 5] [, 6]
# [1, ] 8 32 10 40 12 48
# [2, ] 16 40 20 50 24 60
# [3, ] 24 48 30 60 36 72
# [4, ] 9 36 11 44 13 52
# [5, ] 18 45 22 55 26 65
# [6, ] 27 54 33 66 39 78
```

## Logical / boolean operators

In addition, boolean or logical operators in R are used to specify multiple conditions between objects. These comparisons return `TRUE`

and `FALSE`

values.

Logical operator in R | Description |
---|---|

& | Elementwise logical ‘AND’ |

&& | Vector logical ‘AND’ |

| | Elementwise logical ‘OR’ |

|| | Vector logical ‘OR’ |

! | Logical negation ‘NOT’ |

xor() | Elementwise exclusive ‘OR’ equivalent to !( x | y) |

```
40 & 5 > 30 # FALSE
40 | 5 > 30 # TRUE
!TRUE # FALSE
!FALSE # TRUE
#---------
# Vectors
#---------
x <- c(3, 4, 5)
y <- c(3, 5, 1)
x & y # TRUE TRUE TRUE
x && y # TRUE
x | y # TRUE TRUE TRUE
x || y # TRUE
!x
xor(x, y)
```

## Relational / comparison operators in R

Comparison or relational operators are designed to compare objects and the output of these comparisons are of type boolean. To clarify, the following table summarizes the R relational operators.

Relational operator in R | Description |
---|---|

> | Greater than |

< | Lower than |

>= | Greater or equal than |

<= | Lower or equal than |

== | Equal to |

!= | Not equal to |

For example, you can compare integer values with these operators as follows.

```
#-----------------
# Basic operations
#-----------------
3 > 5 # TRUE
3 < 5 # FALSE
3 >= 5 # FALSE
3 <= 5 # TRUE
3 == 5 # FALSE
3 != 5 # TRUE
```

If you compare vectors the output will be other vector of the same length and each element will contain the boolean corresponding to the comparison of the corresponding elements (the first element of the first vector with the first element of the second vector and so on). Moreover, you can compare each element of a matrix against other.

```
#---------
# Vectors
#---------
x <- c(12, 4 , 14)
y <- c(3, 4, 15)
x >= y # TRUE TRUE FALSE
x <= y # FALSE TRUE TRUE
x == y # FALSE TRUE FALSE
x != y # TRUE FALSE TRUE
#---------
# Matrix
#---------
# Note the matrix must have the same dimension
# The comparison is element by element
s <- matrix(1:6, nrow = 2, ncol = 3)
t <- matrix(8:13, nrow = 2, ncol = 3)
s > t
# [, 1] [, 2] [, 3]
# [1, ] FALSE FALSE FALSE
# [2, ] FALSE FALSE FALSE
s < t
s >= t
s <= t
s == t
```

## Assignment operators in R

The assignment operators in R allows you to **assign data to a named object in order to store the data**.

Assignment operator in R | Description |
---|---|

<- | Left assignment |

= | Left assignment (not recommended) and argument assignment |

-> | Right assignment |

<<- | Left lexicographic assignment (for advanced users) |

->> | Right lexicographic assignment (for advanced users) |

The arrow assignment can be used as left or right assignment, but the right assignment is not generally used. In addition, you can use the double arrow assignment, known as scoping assignment, but we won’t enter in more detail in this tutorial, as it is for advanced users. You can know more about this assignment operator in our post about functions in R.

In the following code block you will find some examples of these operators.

```
x <- 3
x = 26
rnorm(n = 10)
3 -> y
w <<- 7
7 ->> z
```

There are some rules when naming variables. For instance, you can use letters, numbers, dots and underscores in the variable name, but underscores can’t be the first character of the variable name.

There are also reserved words you can’t use, like `TRUE`

, `FALSE`

, `NULL`

, among others. You can see the full list of R reserved words typing `help(Reserved)`

or `?Reserved`

.

However, if for some reason you need to name your variable with a reserved word or starting with an underscore you will need to use backticks:

```
_variable <- 3 # Error
`_variable` <- 3 # Works
TRUE <- 3 # Error
`TRUE` <- 3 # Works
```

## Miscellaneous R operators

Miscellaneous operators in R are operators **used for specific purposes**, as accessing data, functions, creating sequences or specifying a formula of a model. To clarify, the next table contains all the available miscellaneous operators in R.

Miscellaneous operator in R | Description |
---|---|

$ | Named list or dataframe column subset |

: | Sequence generator |

:: | Accessing functions of packages It is not usually needed |

::: | Accessing internal functions of packages |

~ | Model formulae |

@ | Accessing slots in S4 classes (Advanced) |

In addition, in the following block of code we show several examples of these operators:

```
df <- data.frame(x = c(7, 9, 2), y = c(5, 9, 5))
# Accessing the variable x
df$x
# Sequence from 1 to 5
1:5
# rnorm function of the stats package
stats::rnorm(10)
# Linear model formulae
lm(df$x ~ df$y)
```

## Infix operator

**Everything in R is a function** and hence **you can call an operator as a function**. This is known as infix operators. Note that this type of operators are not generally used or needed.

```
`+`(3, 2) # Equivalent to 3 + 2
`*`(5, 9) # Equivalent to 5 * 9
`>`(6, 1) # Equivalent to 6 > 1
```

## Pipe operator in R

The pipe operator is an operator you can find in several libraries, like `dplyr`

. The operator can be read as ‘AND THEN’ and its purpose is to simplify the syntax when writing R code. As an example, you could subset the `cars`

dataset and then create a summary of the subset with the following code:

```
# install.packages("dplyr")
library(dplyr)
cars %>%
subset(speed > 20) %>%
summary()
```

```
speed dist
Min. :22.00 Min. : 54.00
1st Qu.:23.50 1st Qu.: 68.00
Median :24.00 Median : 85.00
Mean :23.71 Mean : 82.86
3rd Qu.:24.00 3rd Qu.: 92.50
Max. :25.00 Max. :120.00
```