R Operators

Learn via video courses
Topics Covered

Overview

In the realm of programming, operators play a vital role in performing various operations on data. R, a popular statistical programming language, provides a rich set of operators that allow users to manipulate and transform data efficiently. These operators in R enable tasks such as arithmetic calculations, logical comparisons, assignment operations, and more.

Understanding and effectively utilizing the operators in R is crucial for writing concise and efficient code. In this article, we will explore the different types of operators available in R and provide examples of each type. By the end, you will have a solid grasp of R operators, enabling you to write powerful and expressive code.

Types of Operators in R

R offers several types of operators, each serving a specific purpose. Let's delve into each type and explore the operators they encompass.

Arithmetic Operators

Arithmetic operators in R are used to perform mathematical calculations on numerical values. These operators include addition, subtraction, multiplication, division, and more. The table below presents the arithmetic operators in R along with their symbols and descriptions:

OperatorDescriptionExample
+Adds two vectors
Subtracts second vector from the firstv <- c( 2,5.5,6) t <- c(8, 3, 4) print(v-t) it produces the following result − [1] -6.0 2.5 2.0
*Multiplies both vectorsv <- c( 2,5.5,6) t <- c(8, 3, 4) print(v*t) it produces the following result − [1] 16.0 16.5 24.0
/Divide the first vector with the secondv <- c( 2,5.5,6) t <- c(8, 3, 4) print(v/t) When we execute the above code, it produces the following result − [1] 0.250000 1.833333 1.500000
%%Give the remainder of the first vector with the secondv <- c( 2,5.5,6) t <- c(8, 3, 4) print(v%%t) it produces the following result − [1] 2.0 2.5 2.0
%/%The result of division of first vector with second (quotient)v <- c( 2,5.5,6) t <- c(8, 3, 4) print(v%/%t) it produces the following result − [1] 0 1 1
^The first vector raised to the exponent of second vectorv <- c( 2,5.5,6) t <- c(8, 3, 4) print(v^t) it produces the following result − [1] 256.000 166.375 1296.000

These arithmetic operators allow you to perform basic mathematical operations on numerical values within R, facilitating calculations and data manipu

Logical Operators

Logical operators in R are used to evaluate logical conditions and return Boolean values (TRUE or FALSE). These operators include logical AND, OR, and NOT. The table below presents the logical operators in R along with their symbols and descriptions:

OperatorDescriptionExample
&It is called Element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if both the elements are TRUE.v <- c(3,1,TRUE,2+3i) t <- c(4,1,FALSE,2+3i) print(v&t) it produces the following result − [1] TRUE TRUE FALSE TRUE
|It is called Element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if one the elements is TRUE.v <- c(3,0,TRUE,2+2i) t <- c(4,0,FALSE,2+3i) print(v|t) it produces the following result − [1] TRUE FALSE TRUE TRUE
!It is called Logical NOT operator. Takes each element of the vector and gives the opposite logical value.v <- c(3,0,TRUE,2+2i) print(!v) it produces the following result − [1] FALSE TRUE FALSE FALSE
&&Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE.v <- c(3,0,TRUE,2+2i) t <- c(1,3,TRUE,2+3i) print(v&&t) it produces the following result − [1] TRUE
||Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE.v <- c(0,0,TRUE,2+2i) t <- c(0,3,TRUE,2+3i) print(v||t) it produces the following result − [1] FALSE

Logical operators in R are particularly useful for constructing complex conditional statements and controlling the flow of your code based on certain conditions.

Relational Operators

Relational operators in R are used to compare values and determine the relationship between them. These operators include less than, greater than, equal to, and more. The table below presents the relational operators in R along with their symbols and descriptions:

OperatorDescriptionExample
>Checks if each element of the first vector is greater than the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v>t) it produces the following result − [1] FALSE TRUE FALSE FALSE
<Checks if each element of the first vector is less than the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v < t) it produces the following result − [1] TRUE FALSE TRUE FALSE
==Checks if each element of the first vector is equal to the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v == t) it produces the following result − [1] FALSE FALSE FALSE TRUE
<=Checks if each element of the first vector is less than or equal to the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v<=t) it produces the following result − [1] TRUE FALSE TRUE TRUE
>=Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v>=t) it produces the following result − [1] FALSE TRUE FALSE TRUE
!=Checks if each element of the first vector is unequal to the corresponding element of the second vector.v <- c(2,5.5,6,9) t <- c(8,2.5,14,9) print(v!=t) it produces the following result − [1] TRUE TRUE TRUE FALSE

Relational operators enable you to compare values and make decisions based on the comparison results, aiding in data filtering, sorting, and conditional operations.

Assignment Operators

Assignment operators in R are used to assign values to variables. They allow you to store data in variables for later use or modification. The table below presents the assignment operators in R along with their symbols and descriptions:

OperatorDescriptionExample
<− or = or <<−Called Left Assignment v1 <- c(3,1,TRUE,2+3i) v2 <<- c(3,1,TRUE,2+3i) v3 = c(3,1,TRUE,2+3i) print(v1) print(v2) print(v3) it produces the following result − [1] 3+0i 1+0i 1+0i 2+3i [1] 3+0i 1+0i 1+0i 2+3i [1] 3+0i 1+0i 1+0i 2+3i
-> or ->>Called Right Assignmentc(3,1,TRUE,2+3i) -> v1 c(3,1,TRUE,2+3i) ->> v2 print(v1) print(v2) it produces the following result − [1] 3+0i 1+0i 1+0i 2+3i [1] 3+0i 1+0i 1+0i 2+3i

Assignment operators are fundamental in R programming as they enable you to store and manipulate data efficiently, facilitating data analysis and processing.

Miscellaneous Operator

Apart from the aforementioned types of operators, R also offers some miscellaneous operators.

OperatorDescriptionExample
: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

Conclusion

  • Operators in R are essential tools for performing operations on data and controlling program flow.
  • R provides various types of operators, including arithmetic, logical, relational, assignment, and miscellaneous operators.
  • Arithmetic operators allow you to perform mathematical calculations on numerical values.
  • Logical operators evaluate logical conditions and return Boolean values.
  • Relational operators compare values and determine their relationships.
  • Assignment operators enable you to store and modify data in variables.
  • Understanding and effectively utilizing operators in R is crucial for writing concise and efficient code.
  • By leveraging the appropriate operators, programmers can perform calculations, implement conditional logic, and manage data effectively.
  • Operators in R are versatile tools that empower programmers to handle data manipulation and analysis tasks efficiently.