SlideShare a Scribd company logo
1 of 29
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 Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to MatricesRsquared 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
 
Import Data using R
Import Data using R Import Data using R
Import Data using R Rupak Roy
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiersNilimesh Halder
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to RstudioOlga Scrivner
 
Joins in databases
Joins in databases Joins in databases
Joins in databases CourseHunt
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation pptWPVKP.COM
 
Functional dependancy
Functional dependancyFunctional dependancy
Functional dependancyVisakh V
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2izahn
 

What's hot (20)

R Programming: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R programming
R programmingR programming
R programming
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
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
 
Import Data using R
Import Data using R Import Data using R
Import Data using R
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
R operators
R   operatorsR   operators
R operators
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to Rstudio
 
R data types
R data typesR data types
R data types
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Joins in databases
Joins in databases Joins in databases
Joins in databases
 
Functional dependency
Functional dependencyFunctional dependency
Functional dependency
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Functional dependancy
Functional dependancyFunctional dependancy
Functional dependancy
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 

Viewers also liked

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 & RangeRsquared Academy
 
Data Visualization With R: Introduction
Data Visualization With R: IntroductionData Visualization With R: Introduction
Data Visualization With R: IntroductionRsquared Academy
 
R Programming: Introduction To R Packages
R Programming: Introduction To R PackagesR Programming: Introduction To R Packages
R Programming: Introduction To R PackagesRsquared Academy
 
R Programming: Getting Help In R
R Programming: Getting Help In RR Programming: Getting Help In R
R Programming: Getting Help In RRsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to VectorsRsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsR Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar PlotsRsquared Academy
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In RR Programming: Numeric Functions In R
R Programming: Numeric Functions In RRsquared Academy
 
R Programming: First Steps
R Programming: First StepsR Programming: First Steps
R Programming: First StepsRsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersRsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRsquared Academy
 

Viewers also liked (10)

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.pdfRahulKumar342376
 
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.pdfMMRF2
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq 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
 
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 KuteTushar B Kute
 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICAemtrajano
 
C Programming Intro.ppt
C Programming Intro.pptC Programming Intro.ppt
C Programming Intro.pptLECO9
 
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.docxcroysierkathey
 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfAbrehamKassa
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptxDeepaRavi21
 

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
 
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
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Python
PythonPython
Python
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
data handling revision.pptx
data handling revision.pptxdata handling revision.pptx
data handling revision.pptx
 

More from Rsquared Academy

Market Basket Analysis in R
Market Basket Analysis in RMarket Basket Analysis in R
Market Basket Analysis in RRsquared 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 RRsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with PipesWriting Readable Code with Pipes
Writing Readable Code with PipesRsquared 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 RRsquared 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 RRsquared 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
 
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 GraphsRsquared 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 PlotsRsquared 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 ParametersRsquared 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 PlotsRsquared Academy
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RRsquared 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 RRsquared 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

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 

Recently uploaded (20)

Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.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