S.
Operator Description Example
No
1. + This operator is used to add two b <- c(11, 5,
3)
vectors in R. a <- c(2, 3.3, 4) 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 b <- c(11, 5,
3)
vector from another one. a <- c(2, print(a-b)
3.3, 4)
It will give us the
following output:
[1] -9.0 -
1.7 3.0
3. * This operator is used to multiply b <- c(11, 5,
3)
two vectors with each other. a <- print(a*b)
c(2, 3.3, 4)
It will give us the
following output:
[1] 22.0
16.5 4.0
4. / This operator divides the vector b <- c(11, 5,
3)
from another one. a <- c(2, 3.3, 4) 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 b <- c(11, 5,
3)
remainder of the first vector with print(a%%b)
the second vector. a <- c(2, 3.3, 4)
It will give us the
following output:
[1] 2.0 3.3
0
6. %/% This operator is used to find the a <- c(2,
3.3, 4)
division of the first vector with the b <- c(11, 5,
second(quotient). 3)
print(a%/%b)
It will give us the
following output:
[1] 0 0 4
7. ^ This operator raised the first vector b <- c(11, 5,
3)
to the exponent of the second print(a^b)
vector. a <- c(2, 3.3, 4)
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. Operator Description Example
No
1. > This operator will return TRUE when a <- c(1,
3, 5)
every element in the first vector is b <- c(2,
greater than the corresponding 4, 6)
element of the second vector. print(a>b)
It will give us
the following
output:
[1] FALSE
FALSE FALSE
2. < This operator will return TRUE when a <- c(1,
9, 5)
every element in the first vector is b <- c(2,
less then the corresponding element 4, 6)
of the second vector. print(a<b)
It will give us
the following
output:
[1] FALSE
TRUE FALSE
3. <= This operator will return TRUE when a <- c(1,
3, 5)
every element in the first vector is b <- c(2,
less than or equal to the 3, 6)
corresponding element of another print(a<=b)
vector.
It will give us
the following
output:
[1] TRUE
TRUE TRUE
4. >= This operator will return TRUE when a <- c(1,
3, 5)
every element in the first vector is b <- c(2,
greater than or equal to the 3, 6)
corresponding element of another print(a>=b)
vector.
It will give us
the following
output:
[1] FALSE
TRUE FALSE
5. == This operator will return TRUE when a <- c(1,
3, 5)
every element in the first vector is b <- c(2,
equal to the corresponding element 3, 6)
of the second vector. print(a==b)
It will give us
the following
output:
[1] FALSE TRUE
FALSE
6. != This operator will return TRUE when a <- c(1,
3, 5)
every element in the first vector is b <- c(2,
not equal to the corresponding 3, 6)
element of the second vector. 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. Operator Description Example
No
1. & This operator is known as the Logical a <- c(3,
0, TRUE, 2+2i)
AND operator. This operator takes b <- c(2,
the first element of both the vector 4, TRUE, 2+3i)
and returns TRUE if both the print(a&b)
elements are TRUE.
It will give us
the following
output:
[1] TRUE
FALSE TRUE TRUE
2. | This operator is called the Logical OR a <- c(3,
0, TRUE, 2+2i)
operator. This operator takes the first b <- c(2,
element of both the vector and 4, TRUE, 2+3i)
returns TRUE if one of them is TRUE. print(a|b)
It will give us
the following
output:
[1] TRUE
TRUE TRUE TRUE
3. ! This operator is known as Logical a <- c(3,
0, TRUE, 2+2i)
NOT operator. This operator takes print(!a)
the first element of the vector and
gives the opposite logical value as a It will give us
result. the following
output:
[1] FALSE
TRUE FALSE FALSE
4. && This operator takes the first element a <- c(3,
0, TRUE, 2+2i)
of both the vector and gives TRUE as b <- c(2,
a result, only if both are TRUE. 4, TRUE, 2+3i)
print(a&&b)
It will give us
the following
output:
[1] TRUE
5. || This operator takes the first element a <- c(3,
0, TRUE, 2+2i)
of both the vector and gives the b <- c(2,
result TRUE, if one of them is true. 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. Operator Description Example
No
1. <- or = These operators are known as a <- c(3, 0,
TRUE, 2+2i)
or <<- left assignment operators. 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 c(3, 0, TRUE,
2+2i) -> a
>> right assignment operators. 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
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. Operator Description Example
No
1. : The colon operator is v <- 1:8
print(v)
used to create the
series of numbers in
sequence for a vector. It will give us the following
output:
[1] 1 2 3 4 5 6 7
8
2. %in% This is used when we a1 <- 8
a2 <- 12
want to identify if an d <- 1:10
element belongs to a print(a1%in%t)
vector. print(a2%in%t)
It will give us the following
output:
[1] FALSE
[1] FALSE
3. %*% It is used to multiply a M=matrix(c(1,2,3,4,5,6),
nrow=2, ncol=3, byrow=TRUE)
matrix with its T=m%*%T(m)
transpose. print(T)
It will give us the following
output:
14 32
32 77