Operators are a fundamental component of every programming language, including the R language. With R operators, you can perform assignments, arithmetic calculations, as well as comparisons and even evaluate logical expressions.
R operators: what is an R operator?¶
The R operators (or R operators in English) are strings of characters or special symbols used to perform operations on values or variables. These operations may include arithmetic calculations, comparisons, assignments, or a range of other actions. Operators play a decisive role in the transformation, manipulation and analyzes of data in R, and are a pillar of R programming.
The R language is distinguished by its multitude of different projects. If you want to put one of your projects online, having your own web hosting may be the solution. At IONOS you can rent web hosting and thus host professional web projects.
What are the different types of R operators?¶
R operators can be divided into different types depending on their functionality. Note that this list is not exhaustive, but contains the main types of operators in R.
- Arithmetic operators in R : they are used for arithmetic calculations.
- Logical operators in R : Logical operators in R are used to compare logical values and evaluate logical expressions. They return a logical value.
- Bitwise operators in R : they allow the manipulation of bits in a number.
- Assignment operators in R : Assignment operators are used to assign values to variables.
- Relational operators in R : This type of R operators is used to compare values and create logical expressions.
Unlike many other programming languages, in R there is no no explicit increment or decrement operators. If you need such functionality in While loops in R or in For loops, you can perform addition or subtraction with 1.
R operators: arithmetic operators¶
Arithmetic operators (arithmetic operators in English) are used to execute mathematical calculationslike basic operations.
Operator | Description | Example | Result |
---|---|---|---|
+
|
Adding numbers | 5 + 3
|
8
|
-
|
Subtraction of numbers | 10 – 5
|
5
|
*
|
Multiplication of numbers | 3* 5
|
15
|
/
|
Division of numbers | 10 / 2
|
5
|
%%
|
Modulo; indicates the remainder of a division | 10%% 4
|
2
|
%/%
|
Division by a whole number | 11 %/% 3
|
3
|
^
|
Exponentiation | 2 ^ 3
|
8
|
Arithmetic R operators in code¶
a <- 7
b <- 3
addition <- a + b
soustraction <- a - b
multiplication <- a * b
division <- a / b
modulo <- a %% b
division par un nombre entier <- a %/% b
exponentiation <- 2^3
R
R operators: software operators¶
Logical operators (software operators in English) are used in R to compare logical values and evaluate logical expressions. They always return a logical value as a result, which can be TRUE Or FALSE.
Operator | Description | Example | Result |
---|---|---|---|
&
|
logical AND; return TRUE if the two values are TRUE. | TRUE & FALSE
|
FALSE
|
` | ` | Pipe operator in R for logical OR; return TRUE if one of the two values is TRUE. | `TRUE |
!
|
NOT logical; inverts the logic value. | !TRUE
|
FALSE
|
Code Examples for Logical Operators in R¶
x <- TRUE
y <- FALSE
opérateur_et <- x & y
opérateur_ou <- x | y
opérateur_non <- !x
R
R operators: bitwise operators¶
Bit operators (bitwise operators in English) allow you to manipulate the bits of a number. To understand how these operators really work, you need in-depth knowledge about the binary system, the base 2 numbering system.
Operator | Description | Example | Result |
---|---|---|---|
bitwAnd
|
AND per bit | bitwAnd(5,3)
|
1
|
bitwOr
|
OR by bit | bitwOr(5,3)
|
7
|
bitwXor
|
XOR (Or exclusive) per bit | bitwXor(5,3)
|
6
|
bitwNot
|
NOT per bit | bitwNot(5)
|
-6
|
bitwShiftL
|
Shift left per bit -> Shift the number of bits indicated in the second parameter, to the left | bitwShiftL(5, 1)
|
10
|
bitwShiftR
|
Right shift per bit -> Shift the number of bits indicated in the first parameter, to the right | bitwShiftR(5, 1)
|
2
|
Bit operators in R code¶
a <- 5
b <- 3
et_par_bit <- bitwAnd(a, b)
ou_par_bit <- bitwOr(a, b)
xor_par_bit <- bitwXor(a, b)
not_par_bit<- bitwNot(a)
décalagegauche<- bitwShiftL(a, 1)
décalagedroite<- bitwShiftR(a, 1)
R
R operators: relational operators¶
Comparison operators (relational operators in English) are used to compare values. They return a Boolean value as a result, i.e. TRUE (true), either FALSE (fake).
Operator | Description | Example | Result |
---|---|---|---|
==
|
Comparing the equality of two values | 5 == 3
|
FALSE
|
!=
|
Comparing the inequality of two values | 5 != 3
|
TRUE` |
<
|
Compare if the value on the left is less than the value on the right. | 5 < 3
|
FALSE
|
>
|
Compare if the value on the left is greater than the value on the right. | 5 > 3
|
TRUE
|
<=
|
Compares whether the value on the left is less than or equal to the value on the right. | 5 <= 3
|
FALSE
|
>=
|
Compares whether the value on the left is greater than or equal to the value on the right. | 5 >= 3
|
TRUE
|
Comparison operators in code¶
x <- 5
y <- 3
égal <- x == y
inégal <- x != y
inférieur_à <- x< y
supérieur_à <- x > y
inférieur_ou_égal_à <- x <= y
supérieur_ou_égal_à <- x >= y
R
R operators: assignment operators¶
The assignment operators (assignment operators in English) are used to assign specific values to variables. They are essential for every programming language. In R, there are different assignment operators, but the operator <-
is however most often used.
Operator | Description | Example | Result |
---|---|---|---|
=
|
Higher level assignment operator, mainly used in functions to assign arguments. | matrix(1, nrow = 2)
|
After execution, there is no named variable nrow. |
<-
|
Arrow assignment operator used to assign simple numeric values or complex values, such as R lists, to variables and create new objects. | matrix(1, nrow <- 2)
|
After execution, there is a variable named nrow. |
<-
|
Assignment operator in functions, which searches for an existing definition of the variable in the environment; if necessary, creates a new variable. | a <<- 1
|
If has Already exists, has now has the value 1, otherwise has is created with the value 1. |
Code examples with assignment operators in R¶
matrix(1, nrow=2)
b <- 3
test <- function() {
a <<- 1
}
R