Operators in R

Operators in R

In this guide we will discuss about Operators in R.

In computer programming, an operator is a symbol which represents an action. An operator is a symbol which tells the compiler to perform specific logical or mathematical manipulations. R programming is very rich in built-in operators.

In R programming, there are different types of operator, and each operator performs a different task. For data manipulation, There are some advance operators also such as model formula and list indexing.

There are the following types of operators used in R:

Operators in R

Arithmetic Operators

Arithmetic operators are the symbols which are used to represent arithmetic math operations. The operators act on each and every element of the vector. There are various arithmetic operators which are supported by R.

S. NoOperatorDescriptionExample
1.+This operator is used to add two vectors in R. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a+b) It will give us the following output: [1] 13.0 8.3 5.0
2.This operator is used to divide a vector from another one. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a-b) It will give us the following output: [1] -9.0 -1.7 3.0
3.*This operator is used to multiply two vectors with each other. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a*b) It will give us the following output: [1] 22.0 16.5 4.0
4./This operator divides the vector from another one. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a/b)It will give us the following output: [1] 0.1818182 0.6600000 4.0000000
5.%%This operator is used to find the remainder of the first vector with the second vector. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a%%b) It will give us the following output: [1] 2.0 3.3 0
6.%/%This operator is used to find the division of the first vector with the second(quotient).a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a%/%b) It will give us the following output: [1] 0 0 4
7.^This operator raised the first vector to the exponent of the second vector. a <- c(2, 3.3, 4)b <- c(11, 5, 3) print(a^b) It will give us the following output: [1] 0248.0000 391.3539 4.0000

Relational Operators

A relational operator is a symbol which defines some kind of relation between two entities. These include numerical equalities and inequalities. A relational operator compares each element of the first vector with the corresponding element of the second vector. The result of the comparison will be a Boolean value. There are the following relational operators which are supported by R:

S. NoOperatorDescriptionExample
1.>This operator will return TRUE when every element in the first vector is greater than the corresponding element of the second vector.a <- c(1, 3, 5) b <- c(2, 4, 6) print(a>b) It will give us the following output: [1] FALSE FALSE FALSE
2.<This operator will return TRUE when every element in the first vector is less then the corresponding element of the second vector.a <- c(1, 9, 5) b <- c(2, 4, 6) print(a<b) It will give us the following output: [1] FALSE TRUE FALSE
3.<=This operator will return TRUE when every element in the first vector is less than or equal to the corresponding element of another vector.a <- c(1, 3, 5) b <- c(2, 3, 6) print(a<=b) It will give us the following output: [1] TRUE TRUE TRUE
4.>=This operator will return TRUE when every element in the first vector is greater than or equal to the corresponding element of another vector.a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us the following output: [1] FALSE TRUE FALSE
5.==This operator will return TRUE when every element in the first vector is equal to the corresponding element of the second vector.a <- c(1, 3, 5) b <- c(2, 3, 6) print(a==b) It will give us the following output:[1] FALSE TRUE FALSE
6.!=This operator will return TRUE when every element in the first vector is not equal to the corresponding element of the second vector.a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It will give us the following output: [1] TRUE FALSE TRUE

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. Logical operators are applicable to those vectors whose type is logical, numeric, or complex.

The logical operator compares each element of the first vector with the corresponding element of the second vector.There are the following types of operators which are supported by R:

S. NoOperatorDescriptionExample
1.&This operator is known as the Logical AND operator. This operator takes the first element of both the vector and returns TRUE if both the elements are TRUE.a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a&b) It will give us the following output: [1] TRUE FALSE TRUE TRUE
2.|This operator is called the Logical OR operator. This operator takes the first element of both the vector and returns TRUE if one of them is TRUE.a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a|b) It will give us the following output: [1] TRUE TRUE TRUE TRUE
3.!This operator is known as Logical NOT operator. This operator takes the first element of the vector and gives the opposite logical value as a result.a <- c(3, 0, TRUE, 2+2i) print(!a) It will give us the following output: [1] FALSE TRUE FALSE FALSE
4.&&This operator takes the first element of both the vector and gives TRUE as a result, only if both are TRUE.a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a&&b) It will give us the following output: [1] TRUE
5.||This operator takes the first element of both the vector and gives the result TRUE, if one of them is true.a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE, 2+3i) print(a||b) It will give us the following output: [1] TRUE

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

S. NoOperatorDescriptionExample
1.<- 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) print(a) print(b) print(d) It will give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i [1] 1+0i 2+0i 1+0i 2+3i
2.-> or ->>These operators are known as right assignment operators.c(3, 0, TRUE, 2+2i) -> a c(2, 4, TRUE, 2+3i) ->> b print(a) print(b) It will give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i

operators which are supported by R:


Miscellaneous Operators

Miscellaneous operators are used for a special and specific purpose. These operators are not used for general mathematical or logical computation. There are the following miscellaneous operators which are supported in R

S. NoOperatorDescriptionExample
1.:The colon operator is used to create the series of numbers in sequence for a vector.v <- 1:8 print(v) It will give us the following output: [1] 1 2 3 4 5 6 7 8
2.%in%This is used when we want to identify if an element belongs to a vector.a1 <- 8 a2 <- 12 d <- 1:10 print(a1%in%t) print(a2%in%t) It will give us the following output: [1] FALSE [1] FALSE
3.%*%It is used to multiply a matrix with its transpose.M=matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE) T=m%*%T(m) print(T) It will give us the following output: 14 32 32 77

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply