SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente (20)

Getting Started with R
Getting Started with RGetting Started with R
Getting Started 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
R programmingR programming
R programming
 
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
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
R studio
R studio R studio
R studio
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
R programming
R programmingR programming
R programming
 
2 it unit-1 start learning r
2 it   unit-1 start learning r2 it   unit-1 start learning r
2 it unit-1 start learning r
 
Arango DB
Arango DBArango DB
Arango DB
 
R programming
R programmingR programming
R programming
 
Programming in R
Programming in RProgramming in R
Programming in R
 
Regression analysis in R
Regression analysis in RRegression analysis in R
Regression analysis in R
 
R programming
R programmingR programming
R programming
 
Introduction to RDF & SPARQL
Introduction to RDF & SPARQLIntroduction to RDF & SPARQL
Introduction to RDF & SPARQL
 
R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
An introduction to R
An introduction to RAn introduction to R
An introduction to R
 

Destacado

R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In RRsquared 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
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching moduleSander 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 Environmentizahn
 
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
 
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 PlatformSyracuse University
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)Dataspora
 
Introduction to R
Introduction to RIntroduction to R
Introduction to RStacy Irwin
 

Destacado (20)

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
 
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
 
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
 
R introduction v2
R introduction v2R introduction v2
R introduction v2
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

Similar a 2 R Tutorial Programming

Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 
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 CSyed 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 structuresPradipta Mishra
 
Verilog Tasks & Functions
Verilog Tasks & FunctionsVerilog Tasks & Functions
Verilog Tasks & Functionsanand hd
 
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 CYi-Hsiu Hsu
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1Marut Singh
 
06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx06-PHPIntroductionserversicebasicss.pptx
06-PHPIntroductionserversicebasicss.pptx20521742
 

Similar a 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
 

Último

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPTBoston Institute of Analytics
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 

Último (20)

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default  Presentation : Data Analysis Project PPTPredictive Analysis for Loan Default  Presentation : Data Analysis Project PPT
Predictive Analysis for Loan Default Presentation : Data Analysis Project PPT
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 

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