Objective: To Impart The Knowledge On
Objective: To Impart The Knowledge On
1. Objective
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
4. Teaching Methodology
5. Evocation
6. Deliverables
Operators in R
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
These operators are used to carry out mathematical operations like addition and
multiplication. Here is a list of arithmetic operators available in R.
Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus (Remainder from division)
%/% Integer Division
Example
> x <- 5
> y <- 16
> x+y
[1] 21
> x-y
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
[1] -11
> x*y
[1] 80
> y/x
[1] 3.2
> y%/%x
[1] 3
> y%%x
[1] 1
> y^x
[1] 1048576
Relational Operators
Relational operators are used to compare between values. Here is a list of relational
operators available in R.
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
Example
> x <- 5
> y <- 16
> x<y
[1] TRUE
> x>y
[1] FALSE
> x<=5
[1] TRUE
> y>=20
[1] FALSE
> y == 16
[1] TRUE
> x != 5
[1] FALSE
Logical operators
Logical operators are used to carry out Boolean operations
like AND, OR etc.
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
|| Logical OR
But && and || examines only the first element of the operands resulting into a
single length logical vector.
Example
Assignment Operators
These operators are used to assign values to variables.
Operator Description
<-, <<-, = Leftwards assignment
->, ->> Rightwards assignment
The <<- operator is used for assigning to variables in the parent environments
(more like global assignments). The rightward assignments, although available are
rarely used.
Example
> x <- 5
>x
[1] 5
>x=9
>x
[1] 9
> 10 -> x
>x
[1] 10
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
Miscellaneous Operators
These operators are used to for specific purpose and not general mathematical
or logical computation.
Operator Description Example
: Colon operator. It
v <- 2:8
creates the series of
numbers in sequence
for a vector. print(v)
[1] 2 3 4 5 6 7 8
%in% This operator is used
v1 <- 8
to identify if an
element belongs to a
vector. v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
[1] TRUE
[1] FALSE
%*% This operator is used
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
to multiply a matrix
with its transpose.
t = M %*% t(M)
print(t)
[,1] [,2]
[1,] 65 82
[2,] 82 117
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
The second formal argument is named header. The = FALSE field means that this argument is
optional, and if we don’t specify it, the default value will be FALSE. If we don’t want the default
value, we must name the argument in our call:
> testscores <- read.table("exams",header=TRUE)
Hence the terminology named argument.
R uses lazy evaluation—it does not evaluate an expression until/unless it needs to—the
named argument may not actually be used.
7. Keywords
Miscellaneous Operators
lazy evaluation
8. Sample Questions
Remember
Understand
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh
11. Student Summary
At the end of this session, the facilitator (Teacher) shall randomly pic-up few students to
summarize the deliverables
The Art of R Programming, Norman Matloff, Cengage Learning, 1st Edition, 2011;Page No: 145-
147
---
Form No. AC 04. 01.2016 (R: 06.10.2016)– GMRIT, Rajam, Andhra Pradesh