0% found this document useful (0 votes)
13 views33 pages

Types of Operators in R Explained

The document provides an overview of R operators, categorizing them into arithmetic, relational, logical, assignment, and miscellaneous operators. It explains the functionality of each type, including examples of how they are used in R programming. The document emphasizes the importance of these operators in performing various mathematical and logical operations on data.

Uploaded by

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

Types of Operators in R Explained

The document provides an overview of R operators, categorizing them into arithmetic, relational, logical, assignment, and miscellaneous operators. It explains the functionality of each type, including examples of how they are used in R programming. The document emphasizes the importance of these operators in performing various mathematical and logical operations on data.

Uploaded by

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

Overview

Topic: ❑ R Operators
❑ Types of Operators in R
Types of ❑ Arithmetic Operators
Operators in ❑ Relational Operators
❑ Logical Operators
R
❑ Assignment Operators
❑ Miscellaneous Operators

Presentation Title
What are 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 Numerical as input operands.
R support majorly four kinds of binary operators between
a set of operands. R language is rich in bult-in operators.

Presentation Title
TYPES OF R-OPERATORS
⮚ Arithmetic Operators
⮚ Logical Operators
⮚ Relational Operators
⮚ Assignment Operators
⮚ Miscellaneous Operators

Presentation Title
ARITHEMETIC
OPERATORS
ARITHMETIC OPERATORS
• Arithmetic operations 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 operations are performed element-wise at the
corresponding positions of the vectors.
• All these R arithmetic operators are binary operators, which
means they operate on two operands.
⮚ Addition Operator(+):

The value at the corresponding positions of both the operands are added.

Input : a<- c(1, 0.1)


b<- c(2.33, 4)
print (a+b)
Output : 3.33
4.10

⮚ Subtraction Operator(-):

The second operand values are subtracted from the first.

Input : a<- 6
b<- 8.4
print(a-b)
Output : -2.4
⮚ Multiplication Operator(*)
The multiplication of corresponding elements of vectors and Integers are multiplied
with the use of ‘*’ operator.

Input : B= matrix(c(4,6i), nrow=1,ncol=2)


C= matrix(c(2,2i),
nrow=1,ncol=2
Print(B*C)
Output : 8+0i -1260i
The element at corresponding positions of matrices are multiplied.

⮚ Division Operator(/)

The first operand is divided by the second operand with the use of ‘/’ operator

Input : a<-1
b<-0
Output ; -Inf
[Link] Operators Operation
1. + Addition
2. - Subtraction
3. * Multiplication
4. / Division
5. %/% Integer Division- Same as
Division, but it return the
integer value by flooring the
extra decimal
6. ^ Exponent- It returns the Power
of One variable against the
other
7. %% Modulus- It returns remainder
after the division
RELATIONAL
OPERATORS
RELATIONAL OPERATORS
• A relational operator is a symbol which defines some kind of
relation between two entities.
• These include numeric equalities and inequalities.
• A relational operator compares each element of first vector with
the corresponding element of the second vector.
• The result of the comparison will be a Boolean Value.
• There are following relational operators which are supported by R:
[Link] Operator Description

1. > This operator will return TRUE when every element in the first vector
is greater than the corresponding element of the second vector.

2. < This operator will return TRUE when every element in the first vector is
less then the corresponding element of the second vector.

3. <= This operator will return TRUE when every element I n the first vector
is less than or equal to the corresponding element of another vector.

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.

5. == The operator will return TRUE when every element in the first vector
is equal to the corresponding element of the second vector.
6. != This operator will return TRUE when every element in the first vector is
not equal to the Corresponding element of the second vector.
LOGICAL
OPERATORS
LOGICAL OPERATORS
• Logical operations simulate element-wise decision operations,
based on the specified operator between the operands, which are
then evaluated to either TRUE or FALSE Boolean value.
• Any non zero integer value is considered as TRUE value, be
it complex or real number.
• The logical operators in R programming are used to combine two or
more conditions, and perform the logical operations using
&(Logical AND), |(Logical OR) and !(Logical NOT).
[Link] Operators Name Description

1. & logical AND It returns true when both conditions are


true

2. && logical AND Same as the above but, it works on


single element

3. | logical OR It return true when at-least one of the


condition is true

4. || logical OR Same as logical OR but, it works on


single element

5. ! Logical NOT If the condition is true, logical NOT


operator returns as false
ASSIGNMENT
OPERATORS
ASSIGNMENT OPERATORS
• Assignment operators are used to assign values to various data
objects in R.
• The objects may be integers, vectors, or functions.
• These values are then stores by the assigned variables names.
• There are two kind of assignment operators: Left and Right.
⮚ Left Assignment (<- or<<-or=) :
Assign a value to a vector.

Input : vec1 = c(“ab”, TRUE)


print (vec1)
Output : “ab” “TRUE”

⮚ Right Assignment(-> or->>) :


Assigns value to a vector.

Input : c(“ab”, TRUE) ->>vec1


print(vec1)
Output : “ab” “TRUE”
MISCELLANEO
US
OPERATORS
Miscellaneous Operators
These are the mixed operators that simulate the printing of
sequences and assignment of vectors, either left or right-handed.
These R programming operators are used for special cases and are
not for general mathematical or logical computation.
⮚ %in% Operator :
Checks if an element belongs to a list and returns a Boolean value TRUE if the value
is present else FALSE.

Input : val <-0.1


list1<-c(TRUE, 0.1, “apple”)
print (val %in =% list1)
Output : TRUE

Checks for the vale 0.1 in the specified list. It exists, therefore, prints TRUE.

⮚ Colon Operator(:) :
Prints a list of elements starting with the element before the color to the element after it.

Input : print(1:5)
Output : 1 2 3 4
5

Prints a sequence of the numbers from 1 to 5.


⮚ %*% 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 first matrix must be equal to number of rows of
second matrix. Multiplication of the matrix A with its transpose, B, produce a square matrix.

Input : 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] #original matrix of order2x3
[1,] 1 2 3
[2,] 2 4 6

[,1] #transposed matrix of order 3x2


[,2]
[1,] 1 2
[2,] 3 4
[3,] 5 [,1]
6 [,2] #product matrix of order2x2
[1,] 35 44
[2,] 44 56
Thank you

Presentation Title

You might also like