In this post, you will learn about various operators in R programming such as Arithmetic, Logical, Comparison, Assignment and Miscellaneous Operators.
Operators are used to perform operations on variables and values.
In the example below, we use the
+ operator to add together two values
> 10 + 5  15 >
Types of Operators in R language –
There are various types of operators in R language –
- Arithmetic Operators
- Logical Operators
- Comparison Operator
- Assignment Operators
- Miscellaneous Operators
1 . Arithmetic Operators –
Arithmetic operators are used with numeric values to perform common mathematical operations.
|+||Addition||x + y|
|–||Subtraction||x – y|
|*||Multiplication||x * y|
|/||Division||x / y|
|^||Exponent||x ^ y|
|%%||Modulus (Remainder from division)||x %% y|
2 . Logical Operators –
The logical operators allow a program to make a decision on the basis of multiple conditions. In the program, each operand is considered as a condition which can be evaluated to a false or true value. The value of the conditions is used to determine the overall value of the op1 operator op2.
The logical operator compares each element of the first vector with the corresponding element of the second vector.
|&||Element-wise Logical AND operator. It returns TRUE if both elements are TRUE|
|&&||Logical AND operator – Returns TRUE if both statements are TRUE|
||||Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE|
|||||Logical OR operator. It returns TRUE if one of the statement is TRUE.|
|!||Logical NOT – returns FALSE if statement is TRUE|
3. Comparison Operators –
Comparison operators are used to compare two values.
|==||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|
4. Assignment Operators –
An assignment operator is used to assign a new value to a variable. In R, these operators are used to assign values to vectors. There are the following types of assignment.
|<- or = or <<-||These operators are known as left assignment operators||a <- c(3, 0, TRUE, 2+2i)|
b <<- c(2, 4, TRUE, 2+3i)
d = c(1, 2, TRUE, 2+3i)
|-> or ->>||These operators are known as right assignment operators.||c(3, 0, TRUE, 2+2i) -> a|
c(2, 4, TRUE, 2+3i) ->> b
5. Miscellaneous Operator –
Miscellaneous operators are used to manipulate data.
|:||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|