SlideShare a Scribd company logo
R Programming
Sakthi Dasan Sekar
R Programming
Operators
R has many operators to carry out different mathematical and logical
operations.
Operators in R can be classified into the following categories.
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
http://shakthydoss.com 2
R Programming
Arithmetic Operators
Arithmetic operators are used for mathematical operations like addition and
multiplication.
Arithmetic Operators in R
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
%% Modulus
%/% Integer Division
http://shakthydoss.com 3
R Programming
Relational operators
Relational operators are used to compare between values.
Relational Operators in R
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
http://shakthydoss.com 4
R Programming
Logical Operators
Logical operators are used to carry out Boolean operations like AND, OR etc.
Logical Operators in R
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
|| Logical OR
http://shakthydoss.com 5
R Programming
Assignment operator
Assigment operators are used to assign values to variables.
Variables are assigned using <- (although = also works)
age <- 18 (left assignment )
18 -> age (right assignment)
http://shakthydoss.com 6
R Programming
if...else statement
Syntax
if (expression) {
statement
}
If the test expression is TRUE, the statement gets executed.
The else part is optional and is evaluated if test expression is FALSE.
age <- 20
if(age > 18){
print("Major")
} else {
print(“Minor”)
}
http://shakthydoss.com 7
R Programming
Nested if...else statement
Only one statement will get executed depending upon the test expressions.
if (expression1) {
statement1
} else if (expression2) {
statement2
} else if (expression3) {
statement3
} else {
statement4
}
http://shakthydoss.com 8
R Programming
ifelse() function
ifelse() function is nothing but a vector equivalent form of if..else.
ifelse(expression, yes, no)
expression– A logical expression, which may be a vector.
yes – What to return if expression is TRUE.
no – What to return if expression is FALSE.
a = c(1,2,3,4)
ifelse(a %% 2 == 0,"even","odd")
http://shakthydoss.com 9
R Programming
• For Loop
For loop in R executes code statements for a particular number of times.
for (val in sequence) {
statement
}
vec <- c(1,2,3,4,5)
for (val in vec) {
print(val)
}
http://shakthydoss.com 10
R Programming
While Loop
while (test_expression) {
statement
}
Here, test expression is evaluated and the body of the loop is entered if the
result is TRUE.
The statements inside the loop are executed and the flow returns to test
expression again.
This is repeated each time until test expression evaluates to FALSE.
http://shakthydoss.com 11
R Programming
break statement
A break statement is used inside a loop to stop the iterations and flow the control
outside of the loop.
num <- 1:5
for (val in num) {
if (val == 3){
break
}
print(val)
}
output 1, 2
http://shakthydoss.com 12
R Programming
next statement
A next statement is useful when you want to skip the current iteration of a loop alone.
num <- 1:5
for (val in num) {
if (val == 3){
next
}
print(val)
}
output 1,2,4,5
http://shakthydoss.com 13
R Programming
repeat loop
A repeat loop is used to iterate over a block of code multiple number of times.
There is no condition check in repeat loop to exit the loop.
You must specify exit condition inside the body of the loop.
Failing to do so will result into an infinite looping.
repeat {
statement
}
http://shakthydoss.com 14
R Programming
switch function
switch function is more like controlled branch of if else statements.
switch (expression, list)
switch(2, "apple", “ball" , "cat")
returns ball.
color = "green"
switch(color, "red"={print("apple")}, "yellow"={print("banana")},
"green"={print("avocado")})
returns avocado
http://shakthydoss.com 15
R Programming
scan() function
scan() function helps to read data from console or file.
reading data from console
x <- scan()
Reading data from file.
x <- scan("http://www.ats.ucla.edu/stat/data/scan.txt", what = list(age = 0,name = ""))
Reading a file using scan function may not be efficient way always.
we will see more handy functions to read files in upcoming chapters.
http://shakthydoss.com 16
R Programming
Running R Script
The source () function instructs R reads the text file and execute its contents.
source("myScript.R")
Optional parameter echo=TRUE will echo the script lines before they are
executed
source("myScript.R", echo=TRUE)
http://shakthydoss.com 17
R Programming
Running a Batch Script
R CMD BATCH command will help to run code in batch mode.
$ R CMD BATCH myscript.R outputfile
In case if you want the output sent to stdout or if you need to pass command-
line arguments to the script then Rscript command can be used.
$ Rscript myScript.R arg1 arg2
http://shakthydoss.com 18
R Programming
Commonly used R functions
append() Add elements to a vector.
c() Combine Values into a Vector or List
identical() Test if 2 objects are exactly equal.
length() Returns length of of R object.
ls() List objects in current environment.
range(x) Returns minimum and maximum of vector.
rep(x,n) Repeat the number x, n times
rev(x) Reversed version of its argument.
seq(x,y,n) Generate regular sequences from x to y, spaced by n
unique(x) Remove duplicate entries from vector
http://shakthydoss.com 19
R Programming
Commonly used R functions
tolower() Convert string to lower case letters
toupper() Convert string to upper case letters
grep() Used for Regular expressions
http://shakthydoss.com 20
R Programming
Commonly used R functions
summary(x) Returns Object Summaries
str(x) Compactly Display the Structure of an Arbitrary R Object
glimpse(x) Compactly Display the Structure of an Arbitrary R Object (dplyr package)
class(x) Return the class of an object.
mode(x) Get or set the type or storage mode of an object.
http://shakthydoss.com 21
R Programming
Knowledge Check
http://shakthydoss.com 22
R Programming
Which of following is valid equation.
A. TRUE %/% FALSE = TRUE
B. (TRUE | FALSE ) & (FALSE != TRUE) == TRUE
C. (TRUE | FALSE ) & (FALSE != TRUE) == FALSE
D. "A" && "a"
Answer B
http://shakthydoss.com 23
R Programming
4 __ 3 = 1. What operator should be used.
A. /
B. *
C. %/%
D. None of the above
Answer C
http://shakthydoss.com 24
R Programming
What will be output ?
age <- 18
18 -> age
print(age)
A. 18
B. Error
C. NA
D. Binary value of 18 will be stored in age.
Answer A
http://shakthydoss.com 25
R Programming
Can if statement be used without an else block.
A. Yes
B. No
Answer A
http://shakthydoss.com 26
R Programming
A break statement is used inside a loop to stop the iterations and flow
the control outside of the loop.
A. TRUE
B. FALSE
Answer A
http://shakthydoss.com 27
R Programming
A next statement is useful when you want to skip the current iteration
of a loop alone.
A. FALSE
B. TRUE
Answer B
http://shakthydoss.com 28
R Programming
Which function should be used to find the length of R object.
A. ls()
B. sizeOf()
C. length()
D. None of the above
Answer C
http://shakthydoss.com 29
R Programming
Display the Structure of an Arbitrary R Object
A. summary(x)
B. str(x)
C. ls(x)
D. None of above
Answer B
http://shakthydoss.com 30
R Programming
A repeat loop is used to iterate over a block of code multiple number of
times. There is no condition check in repeat loop to exit the loop.
A. TRUE
B. FALSE
Answer A
http://shakthydoss.com 31
R Programming
what will be the output of print.
num <- 1:5
for (val in num) {
next
break
print(val)
}
A. Error
B. output 3,4,5
C. Program runs but no output is produced
D. None of the above
Answer C
http://shakthydoss.com 32

More Related Content

What's hot

Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson Algorithm
Adarsh Rotte
 
Non Linear Data Structures
Non Linear Data StructuresNon Linear Data Structures
Non Linear Data Structures
Adarsh Patel
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
MATCHING GRAPH THEORY
MATCHING GRAPH THEORYMATCHING GRAPH THEORY
MATCHING GRAPH THEORY
garishma bhatia
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
Meghaj Mallick
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMSkoolkampus
 
Graph Application in Traffic Control
Graph Application in Traffic ControlGraph Application in Traffic Control
Graph Application in Traffic Control
Muhammadu Isa
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCH
Cool Guy
 
Latex Tutorial by Dr. M. C. Hanumantharaju
Latex Tutorial by Dr. M. C. HanumantharajuLatex Tutorial by Dr. M. C. Hanumantharaju
Latex Tutorial by Dr. M. C. Hanumantharaju
BMS Institute of Technology and Management
 
Turbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse ProgrammingTurbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse Programming
Huzaifa Butt
 
Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceasimnawaz54
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
Paras Intotech
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theoryTech_MX
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In R
Rsquared Academy
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
Mahmoud Shiri Varamini
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
Rsquared Academy
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
1-5 ADS Notes.pdf
1-5 ADS Notes.pdf1-5 ADS Notes.pdf
1-5 ADS Notes.pdf
RameshBabuKellamapal
 

What's hot (20)

Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson Algorithm
 
Non Linear Data Structures
Non Linear Data StructuresNon Linear Data Structures
Non Linear Data Structures
 
Binary Search
Binary SearchBinary Search
Binary Search
 
MATCHING GRAPH THEORY
MATCHING GRAPH THEORYMATCHING GRAPH THEORY
MATCHING GRAPH THEORY
 
Pandas
PandasPandas
Pandas
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
 
Graph Application in Traffic Control
Graph Application in Traffic ControlGraph Application in Traffic Control
Graph Application in Traffic Control
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCH
 
Latex Tutorial by Dr. M. C. Hanumantharaju
Latex Tutorial by Dr. M. C. HanumantharajuLatex Tutorial by Dr. M. C. Hanumantharaju
Latex Tutorial by Dr. M. C. Hanumantharaju
 
Turbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse ProgrammingTurbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse Programming
 
Lecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inferenceLecture 2 predicates quantifiers and rules of inference
Lecture 2 predicates quantifiers and rules of inference
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
MatplotLib.pptx
MatplotLib.pptxMatplotLib.pptx
MatplotLib.pptx
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theory
 
R Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In RR Programming: Transform/Reshape Data In R
R Programming: Transform/Reshape Data In R
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
1-5 ADS Notes.pdf
1-5 ADS Notes.pdf1-5 ADS Notes.pdf
1-5 ADS Notes.pdf
 

Viewers also liked

R programming
R programmingR programming
R programming
Shantanu Patil
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
David Chiu
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
Alberto Labarga
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
Rsquared Academy
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
Dr. Awase Khirni Syed
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in REshwar Sai
 
R Introduction
R IntroductionR Introduction
R Introductionschamber
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Samuel Bosch
 
R tutorial
R tutorialR tutorial
R tutorial
Richard Vidgen
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
Sander Timmer
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environment
izahn
 
LSESU a Taste of R Language Workshop
LSESU a Taste of R Language WorkshopLSESU a Taste of R Language Workshop
LSESU a Taste of R Language Workshop
Korkrid Akepanidtaworn
 
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Goran S. Milovanovic
 
R- Introduction
R- IntroductionR- Introduction
R- Introduction
Venkata Reddy Konasani
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
Syracuse University
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
izahn
 

Viewers also liked (20)

R programming
R programmingR programming
R programming
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
R presentation
R presentationR presentation
R presentation
 
Rtutorial
RtutorialRtutorial
Rtutorial
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
R Introduction
R IntroductionR Introduction
R Introduction
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R tutorial
R tutorialR tutorial
R tutorial
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environment
 
LSESU a Taste of R Language Workshop
LSESU a Taste of R Language WorkshopLSESU a Taste of R Language Workshop
LSESU a Taste of R Language Workshop
 
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
Introduction to R for Data Science :: Session 7 [Multiple Linear Regression i...
 
R- Introduction
R- IntroductionR- Introduction
R- Introduction
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 

Similar to 2 R Tutorial Programming

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
programming9
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
Syed Mustafa
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
Syed Mustafa
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
Pradipta Mishra
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
Pradipta Mishra
 
Programming in C
Programming in CProgramming in C
Programming in C
Nishant Munjal
 
Rcpp
RcppRcpp
Rcpp
Ajay Ohri
 
2. operator
2. operator2. operator
2. operator
Shankar Gangaju
 
Verilog Tasks & Functions
Verilog Tasks & FunctionsVerilog Tasks & Functions
Verilog Tasks & Functions
anand hd
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
Programming Homework Help
 
Proc r
Proc rProc r
Proc r
Ajay Ohri
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
Yi-Hsiu Hsu
 
Presentation 2
Presentation 2Presentation 2
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
Abishek Purushothaman
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
Marut Singh
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
20521742
 
Unit 2
Unit 2Unit 2
Unit 2
DURGADEVIP
 

Similar to 2 R Tutorial Programming (20)

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Oct.22nd.Presentation.Final
Oct.22nd.Presentation.FinalOct.22nd.Presentation.Final
Oct.22nd.Presentation.Final
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Rcpp
RcppRcpp
Rcpp
 
2. operator
2. operator2. operator
2. operator
 
Verilog Tasks & Functions
Verilog Tasks & FunctionsVerilog Tasks & Functions
Verilog Tasks & Functions
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
 
Proc r
Proc rProc r
Proc r
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Unit 2
Unit 2Unit 2
Unit 2
 

Recently uploaded

Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
eddie19851
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
roli9797
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
GetInData
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
ahzuo
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Subhajit Sahu
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 

Recently uploaded (20)

Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
 
Analysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performanceAnalysis insight about a Flyball dog competition team's performance
Analysis insight about a Flyball dog competition team's performance
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdfEnhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
Enhanced Enterprise Intelligence with your personal AI Data Copilot.pdf
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
一比一原版(CBU毕业证)卡普顿大学毕业证如何办理
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTESAdjusting OpenMP PageRank : SHORT REPORT / NOTES
Adjusting OpenMP PageRank : SHORT REPORT / NOTES
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 

2 R Tutorial Programming

  • 2. R Programming Operators R has many operators to carry out different mathematical and logical operations. Operators in R can be classified into the following categories.  Arithmetic operators  Relational operators  Logical operators  Assignment operators http://shakthydoss.com 2
  • 3. R Programming Arithmetic Operators Arithmetic operators are used for mathematical operations like addition and multiplication. Arithmetic Operators in R Operator Description + Addition - Subtraction * Multiplication / Division ^ Exponent %% Modulus %/% Integer Division http://shakthydoss.com 3
  • 4. R Programming Relational operators Relational operators are used to compare between values. Relational Operators in R Operator Description < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to http://shakthydoss.com 4
  • 5. R Programming Logical Operators Logical operators are used to carry out Boolean operations like AND, OR etc. Logical Operators in R Operator Description ! Logical NOT & Element-wise logical AND && Logical AND | Element-wise logical OR || Logical OR http://shakthydoss.com 5
  • 6. R Programming Assignment operator Assigment operators are used to assign values to variables. Variables are assigned using <- (although = also works) age <- 18 (left assignment ) 18 -> age (right assignment) http://shakthydoss.com 6
  • 7. R Programming if...else statement Syntax if (expression) { statement } If the test expression is TRUE, the statement gets executed. The else part is optional and is evaluated if test expression is FALSE. age <- 20 if(age > 18){ print("Major") } else { print(“Minor”) } http://shakthydoss.com 7
  • 8. R Programming Nested if...else statement Only one statement will get executed depending upon the test expressions. if (expression1) { statement1 } else if (expression2) { statement2 } else if (expression3) { statement3 } else { statement4 } http://shakthydoss.com 8
  • 9. R Programming ifelse() function ifelse() function is nothing but a vector equivalent form of if..else. ifelse(expression, yes, no) expression– A logical expression, which may be a vector. yes – What to return if expression is TRUE. no – What to return if expression is FALSE. a = c(1,2,3,4) ifelse(a %% 2 == 0,"even","odd") http://shakthydoss.com 9
  • 10. R Programming • For Loop For loop in R executes code statements for a particular number of times. for (val in sequence) { statement } vec <- c(1,2,3,4,5) for (val in vec) { print(val) } http://shakthydoss.com 10
  • 11. R Programming While Loop while (test_expression) { statement } Here, test expression is evaluated and the body of the loop is entered if the result is TRUE. The statements inside the loop are executed and the flow returns to test expression again. This is repeated each time until test expression evaluates to FALSE. http://shakthydoss.com 11
  • 12. R Programming break statement A break statement is used inside a loop to stop the iterations and flow the control outside of the loop. num <- 1:5 for (val in num) { if (val == 3){ break } print(val) } output 1, 2 http://shakthydoss.com 12
  • 13. R Programming next statement A next statement is useful when you want to skip the current iteration of a loop alone. num <- 1:5 for (val in num) { if (val == 3){ next } print(val) } output 1,2,4,5 http://shakthydoss.com 13
  • 14. R Programming repeat loop A repeat loop is used to iterate over a block of code multiple number of times. There is no condition check in repeat loop to exit the loop. You must specify exit condition inside the body of the loop. Failing to do so will result into an infinite looping. repeat { statement } http://shakthydoss.com 14
  • 15. R Programming switch function switch function is more like controlled branch of if else statements. switch (expression, list) switch(2, "apple", “ball" , "cat") returns ball. color = "green" switch(color, "red"={print("apple")}, "yellow"={print("banana")}, "green"={print("avocado")}) returns avocado http://shakthydoss.com 15
  • 16. R Programming scan() function scan() function helps to read data from console or file. reading data from console x <- scan() Reading data from file. x <- scan("http://www.ats.ucla.edu/stat/data/scan.txt", what = list(age = 0,name = "")) Reading a file using scan function may not be efficient way always. we will see more handy functions to read files in upcoming chapters. http://shakthydoss.com 16
  • 17. R Programming Running R Script The source () function instructs R reads the text file and execute its contents. source("myScript.R") Optional parameter echo=TRUE will echo the script lines before they are executed source("myScript.R", echo=TRUE) http://shakthydoss.com 17
  • 18. R Programming Running a Batch Script R CMD BATCH command will help to run code in batch mode. $ R CMD BATCH myscript.R outputfile In case if you want the output sent to stdout or if you need to pass command- line arguments to the script then Rscript command can be used. $ Rscript myScript.R arg1 arg2 http://shakthydoss.com 18
  • 19. R Programming Commonly used R functions append() Add elements to a vector. c() Combine Values into a Vector or List identical() Test if 2 objects are exactly equal. length() Returns length of of R object. ls() List objects in current environment. range(x) Returns minimum and maximum of vector. rep(x,n) Repeat the number x, n times rev(x) Reversed version of its argument. seq(x,y,n) Generate regular sequences from x to y, spaced by n unique(x) Remove duplicate entries from vector http://shakthydoss.com 19
  • 20. R Programming Commonly used R functions tolower() Convert string to lower case letters toupper() Convert string to upper case letters grep() Used for Regular expressions http://shakthydoss.com 20
  • 21. R Programming Commonly used R functions summary(x) Returns Object Summaries str(x) Compactly Display the Structure of an Arbitrary R Object glimpse(x) Compactly Display the Structure of an Arbitrary R Object (dplyr package) class(x) Return the class of an object. mode(x) Get or set the type or storage mode of an object. http://shakthydoss.com 21
  • 23. R Programming Which of following is valid equation. A. TRUE %/% FALSE = TRUE B. (TRUE | FALSE ) & (FALSE != TRUE) == TRUE C. (TRUE | FALSE ) & (FALSE != TRUE) == FALSE D. "A" && "a" Answer B http://shakthydoss.com 23
  • 24. R Programming 4 __ 3 = 1. What operator should be used. A. / B. * C. %/% D. None of the above Answer C http://shakthydoss.com 24
  • 25. R Programming What will be output ? age <- 18 18 -> age print(age) A. 18 B. Error C. NA D. Binary value of 18 will be stored in age. Answer A http://shakthydoss.com 25
  • 26. R Programming Can if statement be used without an else block. A. Yes B. No Answer A http://shakthydoss.com 26
  • 27. R Programming A break statement is used inside a loop to stop the iterations and flow the control outside of the loop. A. TRUE B. FALSE Answer A http://shakthydoss.com 27
  • 28. R Programming A next statement is useful when you want to skip the current iteration of a loop alone. A. FALSE B. TRUE Answer B http://shakthydoss.com 28
  • 29. R Programming Which function should be used to find the length of R object. A. ls() B. sizeOf() C. length() D. None of the above Answer C http://shakthydoss.com 29
  • 30. R Programming Display the Structure of an Arbitrary R Object A. summary(x) B. str(x) C. ls(x) D. None of above Answer B http://shakthydoss.com 30
  • 31. R Programming A repeat loop is used to iterate over a block of code multiple number of times. There is no condition check in repeat loop to exit the loop. A. TRUE B. FALSE Answer A http://shakthydoss.com 31
  • 32. R Programming what will be the output of print. num <- 1:5 for (val in num) { next break print(val) } A. Error B. output 3,4,5 C. Program runs but no output is produced D. None of the above Answer C http://shakthydoss.com 32