Introduction To R Operators
In a programming language, Operators are symbols that help compiler to perform mathematical and logical operations. The R programming language comes with several built-in operators and supplies. Operators are capable of taking logical and mathematical decisions performed on a set of numbers, numerical, and integers. The R programming language comes with advanced operators like the model formula and list indexing for data manipulation. It mainly supports four types of binary operators.
Types Of R Operators
Listed below are the different types of R operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
1. Arithmetic Operators
With the help of arithmetic operators, one can perform operations for example subtraction, addition, division, multiplication, and modulo using arithmetic operators between elements. It can be scalar values, vectors, or complex numbers. The calculations are performed based on the positions of the elements.
a. Addition operator( ): In the addition operator, both the vectors are added.
Below is the example of an addition operator:
Code:
a <- c (2, 1)
b <- c (3, 4)
print (a b)
Output:
5 5
b. Subtraction operator( – ): In this type of operator, the second vector values are subtracted from the first vector values.
Below is the example of a subtraction operator:
Code:
a <- 7
b <- 2
print ( a - b)
Output:
5
c. Multiplication operator( * ): In the multiplication operator, both vectors are multiplied with the help of the ” * ” operator.
Below is the example of a multiplication operator:
Code:
B= matrix (c(6, 2i) , nrow= 1, ncol= 2)
C= matrix(c(1, 1i) , nrow=1, ncol= 2)
print(B*C)
Output:
[,1] [,2]
[1,] 6 0i -2 0i
Note: Corresponding numbers are multiplied.
d. Division operator( / ): In this operator, the first vector is divided by the second vector with the help of the ‘/’ operator.
Below is the example of a division operator:
Code:
a <- 2
b <- 1
print ( a/b)
Output:
2
e. Power operator( ^ ): The first vector is raised to the power of the second vector.
f. Modulo operator( %%): In this operator remainder of the first vector divided by the second vector is returned.
2. Relational Operators
With the help of relational operators, you can compare operations between two elements. If the first element satisfies the relation compared to the second element, it will return a boolean TRUE value. A TRUE value is greater than the FALSE value.
a. Less than( < ): If the first element is less than the second element, then it will return a TRUE value. Otherwise, it will return a FALSE value.
b. Less than equal to( <= ): If the first element is less than or equal to that of the second element, then it will return a TRUE value. Otherwise, it will return a FALSE value.
c. Greater than( > ): If the first element is greater than the second element, then it will return a TRUE value. Otherwise, it will return a FALSE value.
d. Greater than equal to( >= ): If the first element is greater than or equal to that of the second element, then it will return a TRUE value. Otherwise, it will return a FALSE value.
3. Logical Operators
Given below are the logical operators:
a. Logical AND operator( & ): It will return a TRUE value only if both elements are true.
b.Logical OR operator( | ): It will return a TRUE value if either of the elements is true.
c. NOT operator( ! ): NOT operator is also called a unary operator that nullify the value of elements.
d. Logical AND operator( && ): It will return a TRUE value if both the first elements of the lists are true.
e. Logical OR Operator( || ): It will return a TRUE value if either of the first elements of the lists is true.
4. Assignment Operators
In the R programming language, assignment operators are used to assigning values to different data objects. Objects can include vectors, integers, and functions. There are two types of assignment operators: Right and Left.
Conclusion
In this article, you have seen about different types of operators that are supported by the R programming language. In the R programming language, operators play a crucial role as this is a basic building block.
Recommended Articles
This is a guide to R Operators. Here we discuss the introduction to R operators and the types of R operators like logical operators, arithmetic operators, etc. You can also go through our other suggested articles to learn more–
Are you preparing for the entrance exam ?
Join our Programming Languages test series to get more practice in your preparation
View More