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.

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 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, 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.

Operator Description Example Result
+ Addition of numbers 5 + 3 8
- Subtraction of numbers 10 – 5 5
* Multiplication 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
^ Exponentiation 2 ^ 3 8

Code examples of arithmetic 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 expressions. They always return a truth value as a result, which can be either TRUE or FALSE.

Operator Description 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 manipulate bits in a number. To understand how these operators work, you need in-depth knowledge of the binary system, the number system for base 2.

Operator Description 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 comparison operators in R?

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

Operator Description Example Result
== Compares two values on similarity 5 == 3 FALSE
!= Compares two values on differences 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 comparison 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 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.

Operator Description Example Result
= Top-level assignment operator used primarily in functions to assign arguments matrix(1, nrow = 2) After execution, there is no variable called nrow.
<- Arrow assignment 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.
<<- Assignment operator in functions that searches the environment for an existing variable definition 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 assignment operators in R

matrix(1, nrow=2)
b <- 3
test <- function() {
    a <<- 1
}
R
Was this article helpful?
Page top