R Operators


Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands.
R Operators

R supports majorly four kinds of binary operators between a set of operands. In this article, we will see various types of operators in R Programming language and their usage.

Types of the operator in R language
  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Assignment Operators
  • Miscellaneous Operator

Arithmetic Operators

Arithmetic operations in R simulate various math operations, like addition, subtraction, multiplication, division, and modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The R operators are performed element-wise at the corresponding positions of the vectors.

Addition operator (+)

The values at the corresponding positions of both operands are added. Consider the following R operator snippet to add two numbers and vectors:
a <- 2
b <- 3
print (a+b)
a <- c (1, 0.1)
b <- c (2.33, 4)
print (a+b)
output
[1]5
[1]3.33 4.10

Subtraction Operator (-)

The second operand values are subtracted from the first. Consider the following R operator snippet to subtract two variables or vectors
a <- 6
b <- 8.4
print (a-b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a-b)
output
[1]-2.4
[1] 1 1 1

Multiplication Operator (*)

The multiplication of corresponding elements of vectors and Integers are multiplied with the use of the ‘*’ operator.

a <- 6
b <- 2.5
print (a*b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a*b)
output
[1] 15
[1]  2  6 12

Division Operator (/)

The first operand is divided by the second operand with the use of the ‘/’ operator.
a <- 6
b <- 2
print (a/b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a/b)
output
[1] 3
[1] 2.000000 1.500000 1.333333

Power Operator (^)

The first operand is raised to the power of the second operand.

a <- 3
b <- 2
print (a^b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a^b)
output
[1] 9
[1]  2  9 64

Modulo Operator (%%)

The remainder of the first operand divided by the second operand is returned.
a <- 3
b <- 2
print (a%%b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a%%b)
output
[1] 1
[1] 0 1 1

Integer division( quotient)
a <- 3
b <- 2
print (a%/%b)
a<- c(2,3,4)
b<- c(1,2,3)
print(a%/%b)
output
[1] 1
[1] 2 1 1

Logical Operators

Logical operations in R simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value. Any non-zero integer value is considered as a TRUE value, be it a complex or real number.

Element-wise Logical AND operator (&)

Returns True if both the operands are True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 & list2)
output
[1] FALSE  TRUE

Element-wise Logical OR operator (|)

Returns True if either of the operands is True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1|list2)
output
[1] TRUE TRUE

NOT operator (!)

A unary operator that negates the status of the elements of the operand.
list1 <- c(0,FALSE)
print(!list1)
output
[1] TRUE TRUE


Logical AND operator (&&)

Returns True if both the first elements of the operands are True.( both the conditions are true)
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 && list2)
output
[1] FALSE

Logical OR operator (||)

Returns True if either of the first elements of the operands is True.( if any one of the condition is true)
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1||list2)
output
[1] TRUE


Relational Operators

The relational operators in R carry out comparison operations between the corresponding elements of the operands. Returns a boolean TRUE value if the first operand satisfies the relation compared to the second. A TRUE value is always considered to be greater than the FALSE.

Less than (<)

Returns TRUE if the corresponding element of the first operand is less than that of the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1,"apple")
list2 <- c(0,0.1,"bat")
print(list1<list2)
output
[1] FALSE FALSE  TRUE

Less than equal to (<=)

Returns TRUE if the corresponding element of the first operand is less than or equal to that of the second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")

# Convert lists to character strings
list1_char <- as.character(list1)
list2_char <- as.character(list2)

# Compare character strings
print(list1_char <= list2_char)
output
[1] TRUE TRUE TRUE


Greater than (>)

Returns TRUE if the corresponding element of the first operand is greater than that of the second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")
print(list1 > list2)
output
[1] FALSE FALSE FALSE

Greater than equal to (>=)

Returns TRUE if the corresponding element of the first operand is greater or equal to that of the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")
print(list1 >= list2)
output
[1]  TRUE  TRUE FALSE

Not equal to (!=)

Returns TRUE if the corresponding element of the first operand is not equal to the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1,'apple')
list2 <- c(0,0.1,"bat")
print(list1!=list2)
output
[1]  TRUE FALSE  TRUE


Assignment Operators

Assignment operators in R are used to assigning values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stored by the assigned variable names. There are two kinds of assignment operators: Left and Right

Left Assignment (<- or <<- or =)

Assigns a value to a vector.
x=2
y<-3
z=c(1,2,3)
print(x)
print(y)
print(z)
output
[1] 2
[1] 3
[1] 1 2 3

Right Assignment (-> or ->>)

Assigns value to a vector.

c("ab", TRUE) ->> vec1
print (vec1)
output
[1] "ab"   "TRUE"


Miscellaneous Operators

These are the mixed operators in R that simulate the printing of sequences and assignment of vectors, either left or right-handed.

%in% Operator

Checks if an element belongs to a list and returns a boolean value TRUE if the value is present else FALSE.
val <- 0.1
list1 <- c(TRUE, 0.1,"apple")
print (val %in% list1)
output
[1] TRUE


%*% Operator

This operator is used to multiply a matrix with its transpose. Transpose of the matrix is obtained by interchanging the rows to columns and columns to rows. The number of columns of the first matrix must be equal to the number of rows of the second matrix. Multiplication of the matrix A with its transpose, B, produces a square matrix.
mat = matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
print (mat)
print( t(mat))
pro = mat %*% t(mat)
print(pro)

output
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
[,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[,1] [,2]
[1,]   35   44
[2,]   44   56


: operator

The colon operator is used to create the series of numbers in sequence for a vector.
v<- 1:10
print(v)
output
[1]  1  2  3  4  5  6  7  8  9 10

Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types