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:
| Operator | Name | Example | Example (with values) |
|---|---|---|---|
+ | Addition | x + y | 10 + 5 o/p: 15 |
- | Subtraction | x - y | 10 - 5 o/p: 5 |
* | Multiplication | x * y | 10 * 5 o/p: 50 |
/ | Division | x / y | 10 / 4 o/p: 2.5 |
^ | Exponent | x ^ y | 2 ^ 3 o/p: 8 |
%% | Modulus (Remainder from division) | x %% y | 15 %% 2 o/p: 1 |
%/% | Integer Division / Quotient | x %/% y | 15 %/% 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.
| Operator | Name | Example |
|---|---|---|
<- | Left assignment operator | x <- 10 |
-> | Right assignment operator | 10 -> 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.
| Operator | Name | Example |
|---|---|---|
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= 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.
| Operator | Name | Example |
|---|---|---|
& | Element-wise Logical AND operator. Returns TRUE if both elements are TRUE | x & y |
&& | Logical AND operator - Returns TRUE if both statements are TRUE | x && y |
| | Element-wise Logical OR operator. Returns TRUE if one of the statements is TRUE | x | y |
|| | Logical OR operator. Returns TRUE if one of the statements is TRUE | x || 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.
| Operator | Name | Example |
|---|---|---|
: | Creates a series of numbers in a sequence | x <- 1:10 |
%in% | Find out if an element belongs to a vector | x %in% y |
%*% | Matrix Multiplication | x <- 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