Updated May 26, 2023
Introduction to R Operators
R Operators are the features the R language supports for processing certain operations on the operands or participating variables and data values. The Operators in the R language are of the following types: arithmetic, logical, relational, and assignment operators. The operators in the R language are represented through symbols and processed by the R runtime environment. For example, some of the commonly used arrive, maniac operators are addition operators represented by the ‘+’ symbol for adding two vector values in R, and ‘=’ or ‘<- ‘ is the assignment operator used to assign values to the variable. The R operators help to develop standardized program logic.
Operators
R Operators have many built-in operators. We will look at them one by one. Operators in R can be classified into four categories:
1. Arithmetic Operators
These R operators, as their name suggests, perform operations like addition, subtraction, multiplication, division, exponentiation, modulus, and integer division. The symbols are shown below:
- (+) Addition Operator – It adds two vectors.
- (–) Subtraction Operator – It subtracts the second vector from the first vector.
- (*) Multiplication Operator – It multiplies two vectors.
- (/) Division Operator – It divides the first vector with the second vector.
- (% %) Modulus Operator – It gives the remainder of the first vector with the second.
- (%/%) Integer Division Operator – It gives the quotient of the first vector with the second vector.
- (^) Exponent Operator – The first vector is exponentiated by the second vector.
A few examples are shown below:
x <- 6
> y <- 17
> x+y
[1] 23> x-y
[1] -9> x*y
[1] 102x <- 5
y <- 16
> y/x
[1] 3.2> y%/%x
[1] 3> y%%x
[1] 1These also work on vectors. Here are a few examples of arithmetic operators being used in vectors.
> x <- c(2,8,3)
> y <- c(6,4,1)
> x+y
[1] 8 12 4>x-y
[1] -4 4 22. Relational Operators
As you know by the name, relation operator means the relationship between two values or compare between two values or two operands. Below is the list of symbols, along with their operations :
- (<) Less than Operator – It returns true for elements in the first vector that are less than the corresponding element present in the second vector.
- (>) Greater than Operator – It returns true for elements in the first vector greater than the corresponding element present in the second vector.
- (< =) Less than or Equal to – As the name suggests, It returns true for elements in the first vector which is less than or equal to the corresponding element present in the second vector.
- (> =) Greater than or Equal to – It returns true for elements in the first vector greater than or equal to the corresponding element present in the second vector.
- (= =) Equal to – As the name suggests, It returns true for elements in the first vector equal to the corresponding element present in the second vector.
- (! =) Not Equal to – It returns true for elements in the first vector that are not equal to the corresponding element present in the second vector.
A few examples are shown below:
> y <- 16
> x<y
[1] TRUE> x>y
[1] FALSE> x<=5
[1] TRUE> y>=20
[1] FALSE> y == 16
[1] TRUE> x != 5
[1] FALSEThese also work on vectors. Here are a few examples of arithmetic operators being used in vectors.
> x <- c(2,8,3)
> y <- c(6,4,1)
> x>y
[1] FALSE TRUE TRUE3. Logical Operators
Logical Operators are used for carrying out Boolean operations like AND and OR. Logical vectors are only applicable to vectors of logical type, numeric or complex. The numbers which are more significant than one are true. Below is the list of logical operators and their operations.
- & – It is an element-wise Logical AND Operator. It combines each element of the first vector with the corresponding element of the second vector, and based on the result; it returns TRUE or FALSE.
- | – It is an element-wise Logical OR Operator. Returns TRUE or FALSE. It can return only one of the two available outputs.
- ! – It is Logical, NOT Operator. It works by applying the logical inverse to each element of the vector.
- && – It is Logical AND Operator. Returns TRUE only if both the elements from the vectors are TRUE, and it returns FALSE if any one or both of them are false.
- || – It is a Logical OR Operator. Unlike the previous vector returns TRUE even if one of them is TRUE. It returns false if both are FALSE.
A few examples are shown below:
> x <- c(TRUE,FALSE,0,6)
> y <- c(FALSE,TRUE,FALSE,TRUE)
> !x
[1] FALSE TRUE TRUE FALSE> x&y
[1] FALSE FALSE FALSE TRUE> x&&y
[1] FALSE> x|y
[1] TRUE TRUE FALSE TRUE> x||y
[1] TRUE4. Assignment Operators
Assignment R operators are very simple; they assign values to vectors, as the name suggests. Below is the list of various operators and operations:
- <-,<<-, =: It is called Leftwards assignment operators.
- ->, ->>: It is called Rightward assignment operators.
The operators <- and = can be used to assign variables in the same environment, and the operator <<- is generally used in global assignments.
A few examples are shown below:
> x <- 5
> x
[1] 5> x = 9
> x
[1] 9> 10 -> x
Advantages Of R Operators
So, now let’s conclude this article with the various advantages that R has:
- It is free and open-source.
- It supports a wide variety of extensions, for example, data manipulations, statistical modeling, and graphics.
- They run in every operating system, like Windows, Unix(such as Linux), and Mac.
- It easily connects with other languages, such as connecting and reading from a database using the Open Database Connectivity (ODBC) protocol.
Recommended Articles
This has been a guide to R Operators. Here we discussed the Concept, Various Operators with Examples, and Advantages of R. You can also review our other suggested Articles to learn more.