0% found this document useful (0 votes)
6 views6 pages

R Programming Operators Explained

R programming

Uploaded by

pritammishralvl1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

R Programming Operators Explained

R programming

Uploaded by

pritammishralvl1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

R - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or


logical manipulations. R language is rich in built-in operators and provides following
types of operators.
Types of Operators
We have the following types of operators in R programming −

Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
Arithmetic Operators
Following table shows the arithmetic operators supported by R language. The
operators act on each element of the vector.

Operator Description Example

+ Adds two vectors v <- c(2,5.5,6)


t <- c(8, 3, 4)
print(v+t)
it produces the following result −
[1] 10.0 8.5 10.0

− Subtracts second vector from v <- c( 2,5.5,6)


the first t <- c(8, 3, 4)
print(v-t)
it produces the following result −
[1] -6.0 2.5 2.0

* Multiplies both vectors

v <- 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 v <- c( 2,5.5,6)
second 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 v <- c( 2,5.5,6)


vector with the second t <- c(8, 3, 4)
print(v%%t)
it produces the following result −
[1] 2.0 2.5 2.0

^ The first vector raised to the v <- c( 2,5.5,6)


exponent of second vector t <- c(8, 3, 4)
print(v^t)
it produces the following result −
[1] 256.000 166.375 1296.000

Relational Operators
Following table shows the relational operators supported by R language. Each
element of the first vector is compared with the corresponding element of the second
vector. The result of comparison is a Boolean value.

Operator Description Example

> v <- c(2,5.5,6,9)


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

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

<= v <- c(2,5.5,6,9)


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

>= v <- c(2,5.5,6,9)


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

!= v <- c(2,5.5,6,9)
Checks if each element of the
t <- c(8,2.5,14,9)
first vector is unequal to the
print(v!=t)
corresponding element of the
second vector. it produces the following result −
[1] TRUE TRUE TRUE FALSE
Logical Operators
Following table shows the logical operators supported by R language. It is applicable
only to vectors of type logical, numeric or complex. All numbers greater than 1 are
considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the
second vector. The result of comparison is a Boolean value.
Operator Description Example

& It is called Element-wise Logical v <- c(3,1,TRUE,2+3i)


AND operator. It combines each t <- c(4,1,FALSE,2+3i)
element of the first vector with print(v&t)
the corresponding element of the it produces the following result −
second vector and gives a output
[1] TRUE TRUE FALSE TRUE
TRUE if both the elements are
TRUE.

| It is called Element-wise Logical v <- c(3,0,TRUE,2+2i)


OR operator. It combines each t <- c(4,0,FALSE,2+3i)
element of the first vector with print(v|t)
the corresponding element of the it produces the following result −
second vector and gives a output
[1] TRUE FALSE TRUE TRUE
TRUE if one the elements is
TRUE.

! It is called Logical NOT v <- c(3,0,TRUE,2+2i)


operator. Takes each element of print(!v)
the vector and gives the opposite it produces the following result −
logical value.
[1] FALSE TRUE FALSE FALSE

The logical operator && and || considers only the first element of the vectors and
give a vector of single element as output.

Operator Description Example

&& v <- c(3,0,TRUE,2+2i)


Called Logical AND operator.
t <- c(1,3,TRUE,2+3i)
Takes first element of both the
print(v&&t)
vectors and gives the TRUE only if
both are TRUE. it produces the following result −
[1] TRUE

|| v <- c(0,0,TRUE,2+2i)
Called Logical OR operator. Takes
t <- c(0,3,TRUE,2+3i)
first element of both the vectors
print(v||t)
and gives the TRUE if one of them it produces the following result −
is TRUE.
[1] FALSE

Assignment Operators
These operators are used to assign values to vectors.

Operator Description Example

Called Left Assignment v1 <- c(3,1,TRUE,2+3i)


v2 <<- c(3,1,TRUE,2+3i)
<− v3 = c(3,1,TRUE,2+3i)
or print(v1)
print(v2)
= print(v3)
or 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

Called Right Assignment c(3,1,TRUE,2+3i) -> v1


c(3,1,TRUE,2+3i) ->> v2
-> print(v1)
or 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
Miscellaneous Operators
These operators are used to for specific purpose and not general mathematical or
logical computation.

Operator Description Example

: Colon v <- 2:8


operator. It print(v)
creates the it produces the following result −
series of
numbers in [1] 2 3 4 5 6 7 8
sequence
for a vector.

%in% This v1 <- 8


operator is v2 <- 12
used to t <- 1:10
print(v1 %in% t)
identify if
print(v2 %in% t)
an element
belongs to a it produces the following result −
vector. [1] TRUE
[1] FALSE

%*% This M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow =


operator is TRUE)
used to t = M %*% t(M)
print(t)
multiply a
matrix with it produces the following result −
its [,1] [,2]
transpose. [1,] 65 82
[2,] 82 117

You might also like