# What are R operators?

Operators are fundamental building blocks of any programming language, and R is no exception. You can use R operators to carry out mathematical operations, perform arithmetic calculations, and evaluate comparisons and logical expressions.

## What exactly are R operators?

R operators are **special symbols or strings** that are used to perform operations on values and variables. These operations can include arithmetic calculations, comparisons, assignments or other actions. Operators play a critical role in data transformation, manipulation and analysis in R, and are a cornerstone of [R programming](https://www.ionos.co.uk/digitalguide/websites/web-development/r-programming/).

Tip R is suitable for a variety of projects. If you want to take yours online, a custom webspace could be just what you need. Explore [IONOS’ webspace plans](https://www.ionos.co.uk/hosting/webspace "Webspace providers") and discover versatile hosting possibilities.

## What different types of R operators are there?

R operators can be divided into different groups **according to their functionality**. The list below isn’t exhaustive, but it does include the main types of operators in R.

- **Arithmetic operators**: used for arithmetic calculations.
- **Logical operators**: used to compare truth values and evaluate logical expressions. They return a truth value.
- **Bitwise operators**: used to manipulate bits in a number.
- **Assignment operators**: used to assign values to variables.
- **Comparison operators**: used to compare values and create logical expressions.

Unlike with many other programming languages, there **isn’t an explicit increment or decrement operator** in R. For example, if you need this functionality in for loops or [while loops in R](https://www.ionos.co.uk/digitalguide/websites/web-development/r-while-loop/), you can do so by adding or subtracting with 1.

## What are arithmetic operators in R?

Arithmetic operators are used to perform **mathematical calculations** such as basic arithmetic.

<table>
  <thead>
    <tr>
      <th><strong>Operator</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Example</strong></th>
      <th><strong>Result</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`+`</td>
      <td>Addition of numbers</td>
      <td>`5 + 3`</td>
      <td>`8`</td>
    </tr>
    <tr>
      <td>`-`</td>
      <td>Subtraction of numbers</td>
      <td>`10 – 5`</td>
      <td>`5`</td>
    </tr>
    <tr>
      <td>`*`</td>
      <td>Multiplication of numbers</td>
      <td>`3* 5`</td>
      <td>`15`</td>
    </tr>
    <tr>
      <td>`/`</td>
      <td>Division of numbers</td>
      <td>`10 / 2`</td>
      <td>`5`</td>
    </tr>
    <tr>
      <td>`%%`</td>
      <td>Modulo; returns the rest after a division</td>
      <td>`10%% 4`</td>
      <td>`2`</td>
    </tr>
    <tr>
      <td>`%/%`</td>
      <td>Integer division</td>
      <td>`11 %/% 3`</td>
      <td>`3`</td>
    </tr>
    <tr>
      <td>`^`</td>
      <td>Exponentiation</td>
      <td>`2 ^ 3`</td>
      <td>`8`</td>
    </tr>
  </tbody>
</table>

### Code examples of arithmetic operators in R

```R
a <- 7
b <- 3
addition <- a + b
subtraction <- a - b
multiplication <- a * b
division <- a / b
modulo <- a %% b
Integer division <- a %/% b
exponentiation <- 2^3
```

## What are logical operators in R?

Logical operators in R are used to **compare truth values** and evaluate logical expressions. They always return a truth value as a result, which can be either *TRUE* or *FALSE*.

<table>
  <thead>
    <tr>
      <th><strong>Operator</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Example</strong></th>
      <th><strong>Result</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`&`</td>
      <td>Logical AND; returns *TRUE* if both values are *TRUE*</td>
      <td>`TRUE & FALSE`</td>
      <td>`FALSE`</td>
    </tr>
    <tr>
      <td>`</td><td>`</td>
      <td>Pipe operator in R for logical OR; returns *TRUE* if one of the two values is *TRUE*</td>
      <td>`TRUE </td></tr><tr><td>`!`</td><td>Logical NOT; inverted truth value </td><td>`!TRUE`</td><td>`FALSE`</td>
    </tr>
  </tbody>
</table>

### Code examples of logical operators in R

```R
x <- TRUE
y <- FALSE
and_operator <- x & y
or_operator <- x | y
not_operator <- !x
```

## What are Bitwise operators?

Bitwise operators let you manipulate bits in a number. To understand how these operators work, you need in-depth knowledge of the [binary system](https://www.ionos.co.uk/digitalguide/websites/web-development/binary-code/), the number system for base 2.

<table>
  <thead>
    <tr>
      <th><strong>Operator</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Example</strong></th>
      <th><strong>Result</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`bitwAnd`</td>
      <td>Bitwise AND</td>
      <td>`bitwAnd(5,3)`</td>
      <td>`1`</td>
    </tr>
    <tr>
      <td>`bitwOr`</td>
      <td>Bitwise OR</td>
      <td>`bitwOr(5,3)`</td>
      <td>`7`</td>
    </tr>
    <tr>
      <td>`bitwXor`</td>
      <td>Bitwise XOR (exclusive order)</td>
      <td>`bitwXor(5,3)`</td>
      <td>`6`</td>
    </tr>
    <tr>
      <td>`bitwNot`</td>
      <td>Bitwise NOT</td>
      <td>`bitwNot(5)`</td>
      <td>`-6`</td>
    </tr>
    <tr>
      <td>`bitwShiftL`</td>
      <td>Bitwise left shift -&gt; Bitshift to the left by the number of bits specified in the second parameter</td>
      <td>`bitwShiftL(5, 1)`</td>
      <td>`10`</td>
    </tr>
    <tr>
      <td>`bitwShiftR`</td>
      <td>Bitwise right shift -&gt; Bitshift to the right by the number of bits specified in the first parameter</td>
      <td>`bitwShiftR(5, 1)`</td>
      <td>`2`</td>
    </tr>
  </tbody>
</table>

### Code examples of Bitwise operators in R

```R
a <- 5
b <- 3
bitwise_and <- bitwAnd(a, b)
bitwise_or <- bitwOr(a, b)
bitwise_xor <- bitwXor(a, b)
bitwise_not <- bitwNot(a)
Slide left <- bitwShiftL(a, 1)
Slide right <- bitwShiftR(a, 1)
```

## What are comparison operators in R?

Comparison operators are used to compare values. Here you return a Boolean value, i.e., either *TRUE* or *FALSE*.

<table>
  <thead>
    <tr>
      <th><strong>Operator</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Example</strong></th>
      <th><strong>Result</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`==`</td>
      <td>Compares two values on similarity</td>
      <td>`5 == 3`</td>
      <td>`FALSE`</td>
    </tr>
    <tr>
      <td>`!=`</td>
      <td>Compares two values on differences</td>
      <td>`5 != 3`</td>
      <td>`TRUE`</td>
    </tr>
    <tr>
      <td>`<`</td>
      <td>Compares whether the left value is smaller than the right value</td>
      <td>`5 < 3`</td>
      <td>`FALSE`</td>
    </tr>
    <tr>
      <td>`>`</td>
      <td>Compares whether the left value is greater than the right</td>
      <td>`5 &gt; 3`</td>
      <td>`TRUE`</td>
    </tr>
    <tr>
      <td>`<=`</td>
      <td>Compares whether the left value is smaller than or equal to the value on the right</td>
      <td>`5 <= 3`</td>
      <td>`FALSE`</td>
    </tr>
    <tr>
      <td>`>=`</td>
      <td>Compares whether the left value is greater than or equal to the value on the right</td>
      <td>`5 &gt;= 3`</td>
      <td>`TRUE`</td>
    </tr>
  </tbody>
</table>

### Code examples of comparison operators in R

```R
x <- 5
y <- 3
equal <- x == y
not_equal <- x != y
less_than <- x < y
more_than <- x > y
less_than_or_equal_to <- x <= y
more_than_or_equal_to <- x >= y
```

## What are assignment operators in R?

Assignment operators are used to **assign specific values to variables**. They are an essential part of any programming language. There are several assignment operators in R, but most of the time the `<-` operator is used.

<table>
  <thead>
    <tr>
      <th><strong>Operator</strong></th>
      <th><strong>Description</strong></th>
      <th><strong>Example</strong></th>
      <th><strong>Result</strong></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`=`</td>
      <td>Top-level assignment operator used primarily in functions to assign arguments</td>
      <td>`matrix(1, nrow = 2)`</td>
      <td>After execution, there is no variable called *nrow*.</td>
    </tr>
    <tr>
      <td>`<-`</td>
      <td>Arrow assignment operator used to assign variables simple numeric values or complex values such as [R lists](https://www.ionos.co.uk/digitalguide/websites/web-development/r-lists/) and create new objects</td>
      <td>`matrix(1, nrow <- 2)`</td>
      <td>After execution, there is a variable called *nrow*.</td>
    </tr>
    <tr>
      <td>`<<-`</td>
      <td>Assignment operator in functions that searches the environment for an existing variable definition or otherwise creates a new variable</td>
      <td>`a <<- 1`</td>
      <td>If *a* already exists, *a* now has a value of 1, otherwise *a* is recreated with a value of 1.</td>
    </tr>
  </tbody>
</table>

### Code examples of assignment operators in R

```R
matrix(1, nrow=2)
b <- 3
test <- function() {
    a <<- 1
}
```


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/websites/web-development/r-operators/](https://www.ionos.co.uk/digitalguide/websites/web-development/r-operators/) for AI/LLM consumption.