SlideShare a Scribd company logo
1 of 17
Introduction to R
About R
 World’s most widely used statistics programming language.
 R is a programming language and software environment for statistical analysis,
graphics representation and reporting.
 Created by Ross Ihaka and Robert Gentleman.
 Freely available under the GNU General Public License.
 The core of R is an interpreted computer language which allows branching and
looping as well as modular programming using functions.
 R allows integration with the procedures written in the C, C++, .Net, Python or
FORTRAN languages for efficiency.
Features
 Well-developed, simple and effective programming language
 Includes conditionals, loops, user defined recursive functions and input and output
facilities.
 Effective data handling and storage facility.
 Provides a suite of operators for calculations on arrays, lists, vectors and matrices.
 Provides a large, coherent and integrated collection of tools for data analysis.
 Provides graphical facilities for data analysis and display either directly at the
computer or printing at the papers.
Data Types
 Vectors
 Lists
 Matrices
 Arrays
 Factors
 Data Frames
Variables
 A variable in R can store an atomic vector, group of atomic vectors or a combination of many
Robjects.
 A valid variable name consists of letters, numbers and the dot or underline characters.
 The variable name starts with a letter or the dot not followed by a number.
 A variable itself is not declared of any data type, rather it gets the data type of the R - object
assigned to it.
 We can change a variable’s data type of the same variable again and again when using it in a
program.
 The variables can be assigned values using leftward, rightward and equal to operator
 Eg: var.1 = c(0,1,2,3)
var.2 <- c("learn","R")
c(TRUE,1) -> var.3.
Cont.
 Use ls() function to know all the variables currently available in the workspace
 print(ls())
 Variables can be deleted by using the rm() function.
 rm(var.3)
Operators
 We have the following types of operators in R programming −
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Miscellaneous Operators
Decision making
 if statement : An if statement consists of a Boolean expression followed by one or
more statements.
 if...else statement : An if statement can be followed by an
optional else statement, which executes when the Boolean expression is false.
 switch statement : A switch statement allows a variable to be tested for equality
against a list of values.
Loops
 repeat loop : Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
 Syntax
repeat {
commands
if(condition) {
break
}
}
 while loop : Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
 for loop : Like a while statement, except that it tests the condition at the end of the loop body.
Loop Control Statements
 break statement : Terminates the loop statement and transfers execution to the
statement immediately following the loop.
 Next statement : The next statement simulates the behavior of R switch.
R - Functions
 A function is a set of statements organized together to perform a specific task.
 R has a large number of in-built functions
 User can create their own functions.
 In R, a function is an object so the R interpreter is able to pass control to the
function, along with arguments that may be necessary for the function to
accomplish the actions.
 An R function is created by using the keyword function
Cont.
 Syntax:
function_name <- function(arg_1, arg_2, ...) {
Function body
}
 Eg:
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
Function Components
 Function Name :This is the actual name of the function. It is stored in R
environment as an object with this name.
 Arguments : An argument is a placeholder. When a function is invoked, you pass a
value to the argument. Arguments are optional; that is, a function may contain no
arguments. Also arguments can have default values.
 Function Body : The function body contains a collection of statements that defines
what the function does.
 Return Value : The return value of a function is the last expression in the function
body to be evaluated.
FunctionTypes
 Build in functions
 Simple examples of in-built functions are seq(), mean(), max(), sum(x) and paste(...) etc.
 They are directly called by user written programs.
 User defined functions
 User can create user-defined functions in R according to their needs.
 once created they can be used like the built-in functions.
 Syntax:
new.function <- function(a)
{
for(i in 1:a)
{
b <- i^2
print(b)
}
}
Calling a Function
 Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
 Call the function new.function supplying 6 as an argument.
 new.function(6)
 Calling a Function without an Argument
 new.function()
Strings
 Any value written within a pair of single quote or double quotes in R is treated as a
string
 Internally R stores every string within double quotes, even when you create them with
single quote.
 Rules:
 The quotes at the beginning and end of a string should be both double quotes or both single
quote. They can not be mixed.
 Double quotes can be inserted into a string starting and ending with single quote.
 Single quote can be inserted into a string starting and ending with double quotes.
 Double quotes can not be inserted into a string starting and ending with double quotes.
 Single quote can not be inserted into a string starting and ending with single quote.
Vectors
 Vectors are the most basic R data objects and there are six types of atomic vectors.
 They are logical, integer, double, complex, character and raw.
 Types:
 Single Element Vector
 Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of
the above vector types.
 Multiple Elements Vector
 Using colon operator with numeric data
 Using sequence (Seq.) operator
 Using the c() function

More Related Content

What's hot

What's hot (20)

Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
Programming in Scala - Lecture Four
Programming in Scala - Lecture FourProgramming in Scala - Lecture Four
Programming in Scala - Lecture Four
 
C# Cheat Sheet
C# Cheat SheetC# Cheat Sheet
C# Cheat Sheet
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
 
Basic python part 1
Basic python part 1Basic python part 1
Basic python part 1
 
Array and string
Array and stringArray and string
Array and string
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
LISP:Predicates in lisp
LISP:Predicates in lispLISP:Predicates in lisp
LISP:Predicates in lisp
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Ch13
Ch13Ch13
Ch13
 

Similar to Introduction to R - The World's Most Widely Used Statistics Programming Language

R Programming Language
R Programming LanguageR Programming Language
R Programming LanguageNareshKarela1
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxrajalakshmi5921
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptxranapoonam1
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfDeeptiMalhotra19
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptxSulekhJangra
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R langsenthil0809
 
FULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdfFULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdfattalurilalitha
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdfalaparthi
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptxRhishav Poudyal
 

Similar to Introduction to R - The World's Most Widely Used Statistics Programming Language (20)

Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
R basics for MBA Students[1].pptx
R basics for MBA Students[1].pptxR basics for MBA Students[1].pptx
R basics for MBA Students[1].pptx
 
1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Lecture1
Lecture1Lecture1
Lecture1
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Special topics in finance lecture 2
Special topics in finance   lecture 2Special topics in finance   lecture 2
Special topics in finance lecture 2
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
C language 3
C language 3C language 3
C language 3
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
C language presentation
C language presentationC language presentation
C language presentation
 
functioninpython-1.pptx
functioninpython-1.pptxfunctioninpython-1.pptx
functioninpython-1.pptx
 
Shanks
ShanksShanks
Shanks
 
Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
 
FULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdfFULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdf
 
Functions
FunctionsFunctions
Functions
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
 
User defined function in C.pptx
User defined function in C.pptxUser defined function in C.pptx
User defined function in C.pptx
 

More from Abishek Purushothaman

More from Abishek Purushothaman (7)

Aws solution architect
Aws solution architectAws solution architect
Aws solution architect
 
Machine learning
Machine learningMachine learning
Machine learning
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritance
 
Multiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handlingMultiple Choice Questions for Java interfaces and exception handling
Multiple Choice Questions for Java interfaces and exception handling
 
Mini Project presentation for MCA
Mini Project presentation for MCAMini Project presentation for MCA
Mini Project presentation for MCA
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Exception handling
Exception handlingException handling
Exception handling
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

Introduction to R - The World's Most Widely Used Statistics Programming Language

  • 2. About R  World’s most widely used statistics programming language.  R is a programming language and software environment for statistical analysis, graphics representation and reporting.  Created by Ross Ihaka and Robert Gentleman.  Freely available under the GNU General Public License.  The core of R is an interpreted computer language which allows branching and looping as well as modular programming using functions.  R allows integration with the procedures written in the C, C++, .Net, Python or FORTRAN languages for efficiency.
  • 3. Features  Well-developed, simple and effective programming language  Includes conditionals, loops, user defined recursive functions and input and output facilities.  Effective data handling and storage facility.  Provides a suite of operators for calculations on arrays, lists, vectors and matrices.  Provides a large, coherent and integrated collection of tools for data analysis.  Provides graphical facilities for data analysis and display either directly at the computer or printing at the papers.
  • 4. Data Types  Vectors  Lists  Matrices  Arrays  Factors  Data Frames
  • 5. Variables  A variable in R can store an atomic vector, group of atomic vectors or a combination of many Robjects.  A valid variable name consists of letters, numbers and the dot or underline characters.  The variable name starts with a letter or the dot not followed by a number.  A variable itself is not declared of any data type, rather it gets the data type of the R - object assigned to it.  We can change a variable’s data type of the same variable again and again when using it in a program.  The variables can be assigned values using leftward, rightward and equal to operator  Eg: var.1 = c(0,1,2,3) var.2 <- c("learn","R") c(TRUE,1) -> var.3.
  • 6. Cont.  Use ls() function to know all the variables currently available in the workspace  print(ls())  Variables can be deleted by using the rm() function.  rm(var.3)
  • 7. Operators  We have the following types of operators in R programming −  Arithmetic Operators  Relational Operators  Logical Operators  Assignment Operators  Miscellaneous Operators
  • 8. Decision making  if statement : An if statement consists of a Boolean expression followed by one or more statements.  if...else statement : An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.  switch statement : A switch statement allows a variable to be tested for equality against a list of values.
  • 9. Loops  repeat loop : Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.  Syntax repeat { commands if(condition) { break } }  while loop : Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.  for loop : Like a while statement, except that it tests the condition at the end of the loop body.
  • 10. Loop Control Statements  break statement : Terminates the loop statement and transfers execution to the statement immediately following the loop.  Next statement : The next statement simulates the behavior of R switch.
  • 11. R - Functions  A function is a set of statements organized together to perform a specific task.  R has a large number of in-built functions  User can create their own functions.  In R, a function is an object so the R interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions.  An R function is created by using the keyword function
  • 12. Cont.  Syntax: function_name <- function(arg_1, arg_2, ...) { Function body }  Eg: new.function <- function(a) { for(i in 1:a) { b <- i^2 print(b) } }
  • 13. Function Components  Function Name :This is the actual name of the function. It is stored in R environment as an object with this name.  Arguments : An argument is a placeholder. When a function is invoked, you pass a value to the argument. Arguments are optional; that is, a function may contain no arguments. Also arguments can have default values.  Function Body : The function body contains a collection of statements that defines what the function does.  Return Value : The return value of a function is the last expression in the function body to be evaluated.
  • 14. FunctionTypes  Build in functions  Simple examples of in-built functions are seq(), mean(), max(), sum(x) and paste(...) etc.  They are directly called by user written programs.  User defined functions  User can create user-defined functions in R according to their needs.  once created they can be used like the built-in functions.  Syntax: new.function <- function(a) { for(i in 1:a) { b <- i^2 print(b) } }
  • 15. Calling a Function  Create a function to print squares of numbers in sequence. new.function <- function(a) { for(i in 1:a) { b <- i^2 print(b) } }  Call the function new.function supplying 6 as an argument.  new.function(6)  Calling a Function without an Argument  new.function()
  • 16. Strings  Any value written within a pair of single quote or double quotes in R is treated as a string  Internally R stores every string within double quotes, even when you create them with single quote.  Rules:  The quotes at the beginning and end of a string should be both double quotes or both single quote. They can not be mixed.  Double quotes can be inserted into a string starting and ending with single quote.  Single quote can be inserted into a string starting and ending with double quotes.  Double quotes can not be inserted into a string starting and ending with double quotes.  Single quote can not be inserted into a string starting and ending with single quote.
  • 17. Vectors  Vectors are the most basic R data objects and there are six types of atomic vectors.  They are logical, integer, double, complex, character and raw.  Types:  Single Element Vector  Even when you write just one value in R, it becomes a vector of length 1 and belongs to one of the above vector types.  Multiple Elements Vector  Using colon operator with numeric data  Using sequence (Seq.) operator  Using the c() function