Operators are fun­da­ment­al building blocks of any pro­gram­ming language, and R is no exception. You can use R operators to carry out math­em­at­ic­al op­er­a­tions, perform arith­met­ic cal­cu­la­tions, and evaluate com­par­is­ons and logical ex­pres­sions.

What exactly are R operators?

R operators are special symbols or strings that are used to perform op­er­a­tions on values and variables. These op­er­a­tions can include arith­met­ic cal­cu­la­tions, com­par­is­ons, as­sign­ments or other actions. Operators play a critical role in data trans­form­a­tion, ma­nip­u­la­tion and analysis in R, and are a corner­stone of R pro­gram­ming.

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 and discover versatile hosting pos­sib­il­it­ies.

What different types of R operators are there?

R operators can be divided into different groups according to their func­tion­al­ity. The list below isn’t ex­haust­ive, but it does include the main types of operators in R.

  • Arith­met­ic operators: used for arith­met­ic cal­cu­la­tions.
  • Logical operators: used to compare truth values and evaluate logical ex­pres­sions. They return a truth value.
  • Bitwise operators: used to ma­nip­u­late bits in a number.
  • As­sign­ment operators: used to assign values to variables.
  • Com­par­is­on operators: used to compare values and create logical ex­pres­sions.

Unlike with many other pro­gram­ming languages, there isn’t an explicit increment or decrement operator in R. For example, if you need this func­tion­al­ity in for loops or while loops in R, you can do so by adding or sub­tract­ing with 1.

What are arith­met­ic operators in R?

Arith­met­ic operators are used to perform math­em­at­ic­al cal­cu­la­tions such as basic arith­met­ic.

Operator De­scrip­tion Example Result
+ Addition of numbers 5 + 3 8
- Sub­trac­tion of numbers 10 – 5 5
* Mul­ti­plic­a­tion of numbers 3* 5 15
/ Division of numbers 10 / 2 5
%% Modulo; returns the rest after a division 10%% 4 2
%/% Integer division 11 %/% 3 3
^ Ex­po­nen­ti­ation 2 ^ 3 8

Code examples of arith­met­ic operators in 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
R

What are logical operators in R?

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

Operator De­scrip­tion Example Result
& Logical AND; returns TRUE if both values are TRUE TRUE & FALSE FALSE
` ` Pipe operator in R for logical OR; returns TRUE if one of the two values is TRUE `TRUE
! Logical NOT; inverted truth value !TRUE FALSE

Code examples of logical operators in R

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

What are Bitwise operators?

Bitwise operators let you ma­nip­u­late bits in a number. To un­der­stand how these operators work, you need in-depth knowledge of the binary system, the number system for base 2.

Operator De­scrip­tion Example Result
bitwAnd Bitwise AND bitwAnd(5,3) 1
bitwOr Bitwise OR bitwOr(5,3) 7
bitwXor Bitwise XOR (exclusive order) bitwXor(5,3) 6
bitwNot Bitwise NOT bitwNot(5) -6
bitwShiftL Bitwise left shift -> Bitshift to the left by the number of bits specified in the second parameter bitwShiftL(5, 1) 10
bitwShiftR Bitwise right shift -> Bitshift to the right by the number of bits specified in the first parameter bitwShiftR(5, 1) 2

Code examples of Bitwise operators in 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)
R

What are com­par­is­on operators in R?

Com­par­is­on operators are used to compare values. Here you return a Boolean value, i.e., either TRUE or FALSE.

Operator De­scrip­tion Example Result
== Compares two values on sim­il­ar­ity 5 == 3 FALSE
!= Compares two values on dif­fer­ences 5 != 3 TRUE
< Compares whether the left value is smaller than the right value 5 < 3 FALSE
> Compares whether the left value is greater than the right 5 &gt; 3 TRUE
<= Compares whether the left value is smaller than or equal to the value on the right 5 <= 3 FALSE
>= Compares whether the left value is greater than or equal to the value on the right 5 &gt;= 3 TRUE

Code examples of com­par­is­on operators in 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
R

What are as­sign­ment operators in R?

As­sign­ment operators are used to assign specific values to variables. They are an essential part of any pro­gram­ming language. There are several as­sign­ment operators in R, but most of the time the <- operator is used.

Operator De­scrip­tion Example Result
= Top-level as­sign­ment operator used primarily in functions to assign arguments matrix(1, nrow = 2) After execution, there is no variable called nrow.
<- Arrow as­sign­ment operator used to assign variables simple numeric values or complex values such as R lists and create new objects matrix(1, nrow <- 2) After execution, there is a variable called nrow.
<<- As­sign­ment operator in functions that searches the en­vir­on­ment for an existing variable defin­i­tion or otherwise creates a new variable a <<- 1 If a already exists, a now has a value of 1, otherwise a is recreated with a value of 1.

Code examples of as­sign­ment operators in R

matrix(1, nrow=2)
b <- 3
test <- function() {
    a <<- 1
}
R
Go to Main Menu