SlideShare a Scribd company logo
www.r-squared.in/git-hub
R2 Academy R Programming
Variables & Data Types
R2 AcademyCourse Material
Slide 2
All the material related to this course are available at our website
Slides can be viewed at SlideShare
Scripts can be downloaded from GitHub
Videos can be viewed on our Youtube Channel
R2 AcademyObjectives
Slide 3
Variables
➢ What is a variable?
➢ How to create a variable?
➢ How to assign value?
➢ Understand rules for naming variables
Data Types
➢ Numeric
➢ Integer
➢ Character
➢ Logical
➢ Date/Time
R2 Academy
Slide 4
Variables
Case Study
Suppose you are computing the area of a circle whose radius is 3. In R, you can do this straight
away as shown below:
But you cannot reuse the radius or the area computed in any other computation. Let us see how
variables can change the above scenario and help us in reusing values and computations.
R2 AcademyWhat is a Variable?
Slide 5
➢ Variables are the fundamental elements of any programming language.
➢ They are used to represent values that are likely to change.
➢ They reference memory locations that store information/data.
Let us look at a simple case study to understand variables.
> 3.14 * 3 * 3
[1] 28.26
R2 AcademyCreating Variables
Slide 6
ASSIGNMENT OPERATOR
VARIABLE NAME VARIABLE VALUE
radius <- 3
We can store the value of the radius by creating a variable and assigning it the value. In this
case, we create a variable called radius and assign it the value 3. In the next slide, we will
see how to use this variable in computing the area of a circle.
R2 AcademyUsing Variables
Slide 7
We use variable to store value of radius and pi and use them to compute the area of the
circle. The area computed is itself stored in another variable called area.
R2 AcademyComponents Of A Variable
Slide 8
Components
Name
Memory
Address
Value Data Type
radius 0x6d8d8c8 3 Numeric
R2 Academy
Slide 9
Naming Conventions
Name must begin with
a letter. Do not use
numbers, dollar sign
($) or underscore ( _ ).
The name can contain
numbers or underscore.
Do not use dash (-) or
period (.).
Do not use names of
keywords and avoid
using the names of
built-in functions.
Variables are case
sensitive, so average
and Average would be
different variables.
Use names that are
descriptive. Generally,
variable names should
be nouns.
If name is made of
more than one word,
use underscore ( _ ) to
separate them.
R2 Academy
Slide 10
✓ Variables are the building blocks of a programming language
✓ Variables reference memory locations that store information/data
✓ An assignment operator assigns value to a variable.
✓ A variable has the following components:
○ Name
○ Memory Address
○ Value
○ Data Type
✓ Certain rules must be followed while naming variables
Recap..
R2 Academy
Slide 11
Data Types
R2 AcademyData Types
Slide 12
Data
Numeric Integer Character Logical Date/Time
1.25 1 "hello" TRUE
"2015-10-05
11:41:06 IST"
R2 AcademyNumeric
Slide 13
In R, numbers are represented by the type numeric. We will first create a variable and assign it a value.
After that, we will learn a few methods of checking the type of the variable:
R2 AcademyNumeric
Slide 14
If you have carefully observed, integers are also treated as type numeric. We will learn to create
variables of type integer in a short while. We also learnt a couple of new built in functions:
➢ class
Since R is an object oriented programming language, everything we create, whether it is a variable or a
function, is an object that belongs to a certain class. Hence, the class function returns the class/type
of the variable.
➢ is.numeric
is.numeric tests whether the variable is of type numeric. Note that while the class function returns
the data type, is.numeric returns TRUE or FALSE.
R2 AcademyInteger
Slide 15
Unless specified otherwise, integers are treated as type numeric. In this section, we will learn to create
variables of the type integer and to convert other data types to integer.
➢ First we create a variable number1 and assigned it the value 3 but
when we use the class function, we can see that R treats number1 as
type numeric and not integer.
➢ We then use as.integer to create a second variable number2.
Since we have specified that the variable is of type integer, R treats
number2 as integer.
➢ After that, we use is.integer to test if number1 and number2 are
integers.
R2 AcademyInteger
Slide 16
R2 AcademyInteger
Slide 17
➢ In the last part, we coerce other data types to type integer using as.integer.
R2 AcademyCharacter
Slide 18
Letters, words and group of words are represented by the type character. All data of type character
must be enclosed in single or double quotation marks. In fact, any value enclosed in quotes, will be
treated as character type.
➢ First we create two variables first_name and last_name. In the
first case, the value assigned was enclosed in double quotes, while in
the second case the value assigned was enclosed in single qoutes.
➢ In the next case, we do not use any quotes and R returns an error.
Hence, always ensure that you use single or double quotes while
creating variables of type character.
➢ After that, we use is.character to test if last_name is of type
character.
R2 AcademyCharacter
Slide 19
R2 AcademyCharacter
Slide 20
➢ In the last part, we coerce other data types to type character using as.character.
R2 AcademyLogical
Slide 21
Logical data types take only two values. Either TRUE or FALSE. Such data types are created when we
compare two objects in R using comparison and logical operators.
➢ First we create two variables x and y. They are assigned the values
TRUE and FALSE. After that, we use is.logical to check if they
are of type logical.
➢ In the next case, we use comparison operators and see that their
result is always a logical value.
➢ Finally, we coerce other data types to type logical using
as.logical and see the outcome.
R2 AcademyLogical
Slide 22
R2 AcademyLogical
Slide 23
TRUE is represented by all
numbers except 0. FALSE is
represented only by 0 and
no other numbers
R2 AcademyLogical
Slide 24
We coerce other data types to type logical using as.logical.
R2 AcademyDate/Time
Slide 25
Dates and time are represented by the data type date. We will briefly discuss ways to represent data
that contain dates and time. Use as.Date to coerce data to type date.
R2 AcademyDate/Time
Slide 26
In fact, there are a couple of other ways to represent date and time.
R2 Academy
Slide 27
● Numeric, Integer, Character,Logical and Date are the basic data types in R.
● class/typeof functions returns the data type of an object.
● is.data_type tests whether an object is of the specified data type
○ is.numeric
○ is.integer
○ is.character and
○ is.logical
● as.data_type will coerce objects to the specified data type
○ as.numeric
○ as.integer
○ as.character and
○ as.logical
Summary
R2 AcademyNext Steps...
Slide 28
In the next module:
✓ Understand the concept of vectors
✓ Create vectors of different data types
✓ Coercion of different data types
✓ Perform simple operations on vectors
✓ Handling missing data
✓ Learn to index/subset vectors
R2 Academy
Slide 29
Visit Rsquared Academy
for tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub

More Related Content

What's hot

R studio
R studio R studio
R studio
Kinza Irshad
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regression
Mohit Asija
 
R data types
R data typesR data types
R data types
Teachers Mitraa
 
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
Venkata Reddy Konasani
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
ShareThis
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
Avjinder (Avi) Kaler
 
R data types
R   data typesR   data types
R data types
Learnbay Datascience
 
Data analytics with R
Data analytics with RData analytics with R
Data analytics with R
Dr. C.V. Suresh Babu
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
Rupak Roy
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
FAO
 
R basics
R basicsR basics
R basics
FAO
 
Regression analysis in R
Regression analysis in RRegression analysis in R
Regression analysis in R
Alichy Sowmya
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
Victoria López
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
krishna singh
 
Linear Regression in R
Linear Regression in RLinear Regression in R
Linear Regression in R
Edureka!
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
Alberto Labarga
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
NareshKarela1
 
Simple Linier Regression
Simple Linier RegressionSimple Linier Regression
Simple Linier Regressiondessybudiyanti
 

What's hot (20)

Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
R studio
R studio R studio
R studio
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regression
 
R data types
R data typesR data types
R data types
 
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
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Simple linear regression
Simple linear regressionSimple linear regression
Simple linear regression
 
R data types
R   data typesR   data types
R data types
 
Data analytics with R
Data analytics with RData analytics with R
Data analytics with R
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
R basics
R basicsR basics
R basics
 
Regression analysis in R
Regression analysis in RRegression analysis in R
Regression analysis in R
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
Linear Regression in R
Linear Regression in RLinear Regression in R
Linear Regression in R
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
Simple Linier Regression
Simple Linier RegressionSimple Linier Regression
Simple Linier Regression
 
Reading Data into R
Reading Data into RReading Data into R
Reading Data into R
 

Viewers also liked

R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Rsquared Academy
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
Rsquared Academy
 
R Programming: Introduction To R Packages
R Programming: Introduction To R PackagesR Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
R Programming: Getting Help In R
R Programming: Getting Help In RR Programming: Getting Help In R
R Programming: Getting Help In R
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In RR Programming: Numeric Functions In R
R Programming: Numeric Functions In R
Rsquared Academy
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First Steps
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 

Viewers also liked (11)

R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & RangeData Visualization With R: Learn To Modify Title, Axis Labels & Range
Data Visualization With R: Learn To Modify Title, Axis Labels & Range
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: Introduction
 
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: Getting Help In R
R Programming: Getting Help In RR Programming: Getting Help In R
R Programming: Getting Help In R
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In RR Programming: Numeric Functions In R
R Programming: Numeric Functions In R
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First Steps
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 

Similar to R Programming: Variables & Data Types

Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
RahulKumar342376
 
PYTHON.pdf
PYTHON.pdfPYTHON.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
MMRF2
 
R Programming: Variable
R Programming: VariableR Programming: Variable
R Programming: Variable
AvinabaMukherjee6
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
Tareq Hasan
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Jitendra Bafna
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
Adewumi Ezekiel Adebayo
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
emtrajano
 
C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
LECO9
 
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docxMaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
croysierkathey
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
AbrehamKassa
 
Module 4.pptx
Module 4.pptxModule 4.pptx
Module 4.pptx
charancherry185493
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
InfinityWorld3
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
floraaluoch3
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
Python
PythonPython

Similar to R Programming: Variables & Data Types (20)

Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
 
PYTHON.pdf
PYTHON.pdfPYTHON.pdf
PYTHON.pdf
 
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdfL2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
L2 C# Programming Comments, Keywords, Identifiers, Variables.pdf
 
Savitch Ch 17
Savitch Ch 17Savitch Ch 17
Savitch Ch 17
 
R Programming: Variable
R Programming: VariableR Programming: Variable
R Programming: Variable
 
Savitch ch 17
Savitch ch 17Savitch ch 17
Savitch ch 17
 
Variables & Data Types in R
Variables & Data Types in RVariables & Data Types in R
Variables & Data Types in R
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
Engineering Student MuleSoft Meetup#6 - Basic Understanding of DataWeave With...
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
 
C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.ppt
 
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docxMaciasWinter 2020ENG 107Project #2A Year in Gami.docx
MaciasWinter 2020ENG 107Project #2A Year in Gami.docx
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
 
Module 4.pptx
Module 4.pptxModule 4.pptx
Module 4.pptx
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Python
PythonPython
Python
 

More from Rsquared Academy

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
Rsquared Academy
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
Rsquared Academy
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
Rsquared Academy
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
Rsquared Academy
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Rsquared Academy
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
Rsquared Academy
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
Rsquared Academy
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Rsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
Rsquared Academy
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
Rsquared Academy
 
R Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In RR Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In R
Rsquared Academy
 

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in RHandling Date & Time in R
Handling Date & Time in R
 
Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in R
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using RPractical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
 
Joining Data with dplyr
Joining Data with dplyrJoining Data with dplyr
Joining Data with dplyr
 
Explore Data using dplyr
Explore Data using dplyrExplore Data using dplyr
Explore Data using dplyr
 
Data Wrangling with dplyr
Data Wrangling with dplyrData Wrangling with dplyr
Data Wrangling with dplyr
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with Pipes
 
Introduction to tibbles
Introduction to tibblesIntroduction to tibbles
Introduction to tibbles
 
Read data from Excel spreadsheets into R
Read data from Excel spreadsheets into RRead data from Excel spreadsheets into R
Read data from Excel spreadsheets into R
 
Read/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into RRead/Import data from flat/delimited files into R
Read/Import data from flat/delimited files into R
 
How to install & update R packages?
How to install & update R packages?How to install & update R packages?
How to install & update R packages?
 
How to get help in R?
How to get help in R?How to get help in R?
How to get help in R?
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Data Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple GraphsData Visualization With R: Learn To Combine Multiple Graphs
Data Visualization With R: Learn To Combine Multiple Graphs
 
R Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To PlotsR Data Visualization: Learn To Add Text Annotations To Plots
R Data Visualization: Learn To Add Text Annotations To Plots
 
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical ParametersData Visualization With R: Learn To Modify Font Of Graphical Parameters
Data Visualization With R: Learn To Modify Font Of Graphical Parameters
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of PlotsData Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
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: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In RR Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In R
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 

R Programming: Variables & Data Types

  • 1. www.r-squared.in/git-hub R2 Academy R Programming Variables & Data Types
  • 2. R2 AcademyCourse Material Slide 2 All the material related to this course are available at our website Slides can be viewed at SlideShare Scripts can be downloaded from GitHub Videos can be viewed on our Youtube Channel
  • 3. R2 AcademyObjectives Slide 3 Variables ➢ What is a variable? ➢ How to create a variable? ➢ How to assign value? ➢ Understand rules for naming variables Data Types ➢ Numeric ➢ Integer ➢ Character ➢ Logical ➢ Date/Time
  • 5. Case Study Suppose you are computing the area of a circle whose radius is 3. In R, you can do this straight away as shown below: But you cannot reuse the radius or the area computed in any other computation. Let us see how variables can change the above scenario and help us in reusing values and computations. R2 AcademyWhat is a Variable? Slide 5 ➢ Variables are the fundamental elements of any programming language. ➢ They are used to represent values that are likely to change. ➢ They reference memory locations that store information/data. Let us look at a simple case study to understand variables. > 3.14 * 3 * 3 [1] 28.26
  • 6. R2 AcademyCreating Variables Slide 6 ASSIGNMENT OPERATOR VARIABLE NAME VARIABLE VALUE radius <- 3 We can store the value of the radius by creating a variable and assigning it the value. In this case, we create a variable called radius and assign it the value 3. In the next slide, we will see how to use this variable in computing the area of a circle.
  • 7. R2 AcademyUsing Variables Slide 7 We use variable to store value of radius and pi and use them to compute the area of the circle. The area computed is itself stored in another variable called area.
  • 8. R2 AcademyComponents Of A Variable Slide 8 Components Name Memory Address Value Data Type radius 0x6d8d8c8 3 Numeric
  • 9. R2 Academy Slide 9 Naming Conventions Name must begin with a letter. Do not use numbers, dollar sign ($) or underscore ( _ ). The name can contain numbers or underscore. Do not use dash (-) or period (.). Do not use names of keywords and avoid using the names of built-in functions. Variables are case sensitive, so average and Average would be different variables. Use names that are descriptive. Generally, variable names should be nouns. If name is made of more than one word, use underscore ( _ ) to separate them.
  • 10. R2 Academy Slide 10 ✓ Variables are the building blocks of a programming language ✓ Variables reference memory locations that store information/data ✓ An assignment operator assigns value to a variable. ✓ A variable has the following components: ○ Name ○ Memory Address ○ Value ○ Data Type ✓ Certain rules must be followed while naming variables Recap..
  • 12. R2 AcademyData Types Slide 12 Data Numeric Integer Character Logical Date/Time 1.25 1 "hello" TRUE "2015-10-05 11:41:06 IST"
  • 13. R2 AcademyNumeric Slide 13 In R, numbers are represented by the type numeric. We will first create a variable and assign it a value. After that, we will learn a few methods of checking the type of the variable:
  • 14. R2 AcademyNumeric Slide 14 If you have carefully observed, integers are also treated as type numeric. We will learn to create variables of type integer in a short while. We also learnt a couple of new built in functions: ➢ class Since R is an object oriented programming language, everything we create, whether it is a variable or a function, is an object that belongs to a certain class. Hence, the class function returns the class/type of the variable. ➢ is.numeric is.numeric tests whether the variable is of type numeric. Note that while the class function returns the data type, is.numeric returns TRUE or FALSE.
  • 15. R2 AcademyInteger Slide 15 Unless specified otherwise, integers are treated as type numeric. In this section, we will learn to create variables of the type integer and to convert other data types to integer. ➢ First we create a variable number1 and assigned it the value 3 but when we use the class function, we can see that R treats number1 as type numeric and not integer. ➢ We then use as.integer to create a second variable number2. Since we have specified that the variable is of type integer, R treats number2 as integer. ➢ After that, we use is.integer to test if number1 and number2 are integers.
  • 17. R2 AcademyInteger Slide 17 ➢ In the last part, we coerce other data types to type integer using as.integer.
  • 18. R2 AcademyCharacter Slide 18 Letters, words and group of words are represented by the type character. All data of type character must be enclosed in single or double quotation marks. In fact, any value enclosed in quotes, will be treated as character type. ➢ First we create two variables first_name and last_name. In the first case, the value assigned was enclosed in double quotes, while in the second case the value assigned was enclosed in single qoutes. ➢ In the next case, we do not use any quotes and R returns an error. Hence, always ensure that you use single or double quotes while creating variables of type character. ➢ After that, we use is.character to test if last_name is of type character.
  • 20. R2 AcademyCharacter Slide 20 ➢ In the last part, we coerce other data types to type character using as.character.
  • 21. R2 AcademyLogical Slide 21 Logical data types take only two values. Either TRUE or FALSE. Such data types are created when we compare two objects in R using comparison and logical operators. ➢ First we create two variables x and y. They are assigned the values TRUE and FALSE. After that, we use is.logical to check if they are of type logical. ➢ In the next case, we use comparison operators and see that their result is always a logical value. ➢ Finally, we coerce other data types to type logical using as.logical and see the outcome.
  • 23. R2 AcademyLogical Slide 23 TRUE is represented by all numbers except 0. FALSE is represented only by 0 and no other numbers
  • 24. R2 AcademyLogical Slide 24 We coerce other data types to type logical using as.logical.
  • 25. R2 AcademyDate/Time Slide 25 Dates and time are represented by the data type date. We will briefly discuss ways to represent data that contain dates and time. Use as.Date to coerce data to type date.
  • 26. R2 AcademyDate/Time Slide 26 In fact, there are a couple of other ways to represent date and time.
  • 27. R2 Academy Slide 27 ● Numeric, Integer, Character,Logical and Date are the basic data types in R. ● class/typeof functions returns the data type of an object. ● is.data_type tests whether an object is of the specified data type ○ is.numeric ○ is.integer ○ is.character and ○ is.logical ● as.data_type will coerce objects to the specified data type ○ as.numeric ○ as.integer ○ as.character and ○ as.logical Summary
  • 28. R2 AcademyNext Steps... Slide 28 In the next module: ✓ Understand the concept of vectors ✓ Create vectors of different data types ✓ Coercion of different data types ✓ Perform simple operations on vectors ✓ Handling missing data ✓ Learn to index/subset vectors
  • 29. R2 Academy Slide 29 Visit Rsquared Academy for tutorials on: → R Programming → Business Analytics → Data Visualization → Web Applications → Package Development → Git & GitHub