SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
Nimrita Koul
 

Was ist angesagt? (20)

Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
R Programming: Introduction To R Packages
R Programming: Introduction To R PackagesR Programming: Introduction To R Packages
R Programming: Introduction To R Packages
 
R programming
R programmingR programming
R programming
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
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
 
R programming
R programmingR programming
R programming
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Data Mining with R programming
Data Mining with R programmingData Mining with R programming
Data Mining with R programming
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operations
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 
R Programming
R ProgrammingR Programming
R Programming
 
R programming Language
R programming LanguageR programming Language
R programming Language
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
Data Exploration and Visualization with R
Data Exploration and Visualization with RData Exploration and Visualization with R
Data Exploration and Visualization with R
 

Andere mochten auch

Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
Eshwar Sai
 
R Introduction
R IntroductionR Introduction
R Introduction
schamber
 

Andere mochten auch (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
 
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
 
R Functions
R FunctionsR Functions
R Functions
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 

Ähnlich wie 2 R Tutorial Programming

course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
webhostingguy
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx
20521742
 

Ähnlich wie 2 R Tutorial Programming (20)

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
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
 
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
 

Kürzlich hochgeladen

Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Abortion pills in Riyadh +966572737505 get cytotec
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
q6pzkpark
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
ptikerjasaptiker
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
wsppdmt
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
vexqp
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
vexqp
 

Kürzlich hochgeladen (20)

Data Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdfData Analyst Tasks to do the internship.pdf
Data Analyst Tasks to do the internship.pdf
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit RiyadhCytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
Cytotec in Jeddah+966572737505) get unwanted pregnancy kit Riyadh
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptxThe-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
The-boAt-Story-Navigating-the-Waves-of-Innovation.pptx
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
 
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
怎样办理纽约州立大学宾汉姆顿分校毕业证(SUNY-Bin毕业证书)成绩单学校原版复制
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
 

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