📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Operators in R

In programming, an operator is a symbol which represents an action. In another words, an operator is a symbol which is used to perform operation on variables and values.

R supports the following operators:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Miscellaneous operators

Arithmetic operators:

Arithmetic operators are the symbols which are used to perform arithmetical operations. R supports following arithmetic operators:

OperatorNameExampleExample (with values)
+Additionx + y10 + 5 o/p: 15
-Subtractionx - y10 - 5 o/p: 5
*Multiplicationx * y10 * 5 o/p: 50
/Divisionx / y10 / 4 o/p: 2.5
^Exponentx ^ y2 ^ 3 o/p: 8
%%Modulus (Remainder from division)x %% y15 %% 2 o/p: 1
%/%Integer Division / Quotientx %/% y15 %/% 2 o/p: 7

Example:

Filename: arith_op.R

# Define two numbers
a <- 15
b <- 2

add_result <- a + b          # Addition
cat("Addition (a + b):", add_result, "\n")

sub_result <- a - b          # Subtraction
cat("Subtraction (a - b):", sub_result, "\n")

mul_result <- a * b          # Multiplication
cat("Multiplication (a * b):", mul_result, "\n")

div_result <- a / b          # Division
cat("Division (a / b):", div_result, "\n")

exp_result <- a ^ b          # Exponentiation
cat("Exponentiation (a ^ b):", exp_result, "\n")

mod_result <- a %% b         # Modulus
cat("Modulus (a %% b):", mod_result, "\n")

int_div_result <- a %/% b    # Integer Division
cat("Integer Division (a %/% b):", int_div_result, "\n")

Output: $ Rscript arith_op.R

Addition (a + b): 17
Subtraction (a - b): 13
Multiplication (a * b): 30
Division (a / b): 7.5
Exponentiation (a ^ b): 225
Modulus (a %% b): 1
Integer Division (a %/% b): 7

Assignment operators:

Assignment operators are used to assign values to variables.

OperatorNameExample
<-Left assignment operatorx <- 10
->Right assignment operator10 -> x

Example:

Filename: assign_op.R

x <- 30
print(x)

40 -> y
print(y)

Output: $ Rscript assign_op.R

[1] 30
[1] 40

Comparison / Relational operators:

Comparison / Relational operators are used to compare two values or variables.

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Example:

Filename: relational_op.R

a <- 10
b <- 20

cat("a = ", a, "\n")
cat("b = ", b, "\n")

cat("Is a equal to b? :", (a == b), "\n")                  # Equal to (==)
cat("Is a not equal to b? :", (a != b), "\n")               # Not equal to (!=)
cat("Is a greater than b? :", (a > b), "\n")               # Greater than (>)
cat("Is a less than b? :", (a < b), "\n")                  # Less than (<)
cat("Is a greater than or equal to b? :", (a >= b), "\n")  # Greater than or equal to (>=)
cat("Is a less than or equal to b? :", (a <= b), "\n")     # Less than or equal to (<=)

Output: $ Rscript relational_op.R

a = 10
b = 20
Is a equal to b? : FALSE
Is a not equal to b? : TRUE
Is a greater than b? : FALSE
Is a less than b? : TRUE
Is a greater than or equal to b? : FALSE
Is a less than or equal to b? : TRUE

Logical operators:

The logical operators allow a program to decide based on multiple conditions. In simple words, Logical operators are used to combine two or more conditional statements.

OperatorNameExample
&Element-wise Logical AND operator. Returns TRUE if both elements are TRUEx & y
&&Logical AND operator - Returns TRUE if both statements are TRUEx && y
|Element-wise Logical OR operator. Returns TRUE if one of the statements is TRUEx | y
||Logical OR operator. Returns TRUE if one of the statements is TRUEx || y
!Logical NOT - Returns FALSE if statement is TRUE!x

Example:

Filename: logical_op.R

x <- TRUE
y <- FALSE

# Logical AND (&)
cat("Logical AND (x & y):", x & y, "\n")

# Logical OR (|)
cat("Logical OR (x | y):", x | y, "\n")

# Logical NOT (!)
cat("Logical NOT (!x):", !x, "\n")

# Logical AND with short-circuiting (&&)
cat("Short-circuit AND (x && y):", x && y, "\n")

# Logical OR with short-circuiting (||)
cat("Short-circuit OR (x || y):", x || y, "\n")

Output: $ Rscript logical_op.R

Logical AND (x & y): FALSE
Logical OR (x | y): TRUE
Logical NOT (!x): FALSE
Short-circuit AND (x && y): FALSE
Short-circuit OR (x || y): TRUE

Miscellaneous operators:

Miscellaneous operators are used to manipulate data. In other words, Miscellaneous operators are used for a special and specific purpose.

OperatorNameExample
:Creates a series of numbers in a sequencex <- 1:10
%in%Find out if an element belongs to a vectorx %in% y
%*%Matrix Multiplicationx <- Matrix1 %*% Matrix2

Example:

Filename: miscellaneous_op.R

x <- 1:10
print(x)

print(3 %in% x)
print(12 %in% x)

Output: $ Rscript miscellaneous_op.R

[1]  1  2  3  4  5  6  7  8  9 10
[1] TRUE
[1] FALSE