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
Ad

Recommended

Data Management in R
Data Management in R
Sankhya_Analytics
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Variables & Data Types in R
Variables & Data Types in R
Rsquared Academy
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
Data analysis with R
Data analysis with R
ShareThis
 
Getting Started with R
Getting Started with R
Sankhya_Analytics
 
An Introduction to Data Mining with R
An Introduction to Data Mining with R
Yanchang Zhao
 
R Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
Step By Step Guide to Learn R
Step By Step Guide to Learn R
Venkata Reddy Konasani
 
Arrays in python
Arrays in python
moazamali28
 
Introduction to R and R Studio
Introduction to R and R Studio
Rupak Roy
 
R data-import, data-export
R data-import, data-export
FAO
 
Loops and functions in r
Loops and functions in r
manikanta361
 
Introduction to R
Introduction to R
Ajay Ohri
 
R data types
R data types
Learnbay Datascience
 
Data visualization using R
Data visualization using R
Ummiya Mohammedi
 
R Programming: Importing Data In R
R Programming: Importing Data In R
Rsquared Academy
 
Association rule mining.pptx
Association rule mining.pptx
maha797959
 
Least Squares Regression Method | Edureka
Least Squares Regression Method | Edureka
Edureka!
 
R Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
Pandas
Pandas
maikroeder
 
R Programming Language
R Programming Language
NareshKarela1
 
Data frame operations
Data frame operations
19MSS011dhanyatha
 
Data types in python
Data types in python
RaginiJain21
 
Statistics for data scientists
Statistics for data scientists
Ajay Ohri
 
Numpy
Numpy
Jyoti shukla
 
Introduction to matplotlib
Introduction to matplotlib
Piyush rai
 
Data 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: Introduction
Rsquared Academy
 

More Related Content

What's hot (20)

Step By Step Guide to Learn R
Step By Step Guide to Learn R
Venkata Reddy Konasani
 
Arrays in python
Arrays in python
moazamali28
 
Introduction to R and R Studio
Introduction to R and R Studio
Rupak Roy
 
R data-import, data-export
R data-import, data-export
FAO
 
Loops and functions in r
Loops and functions in r
manikanta361
 
Introduction to R
Introduction to R
Ajay Ohri
 
R data types
R data types
Learnbay Datascience
 
Data visualization using R
Data visualization using R
Ummiya Mohammedi
 
R Programming: Importing Data In R
R Programming: Importing Data In R
Rsquared Academy
 
Association rule mining.pptx
Association rule mining.pptx
maha797959
 
Least Squares Regression Method | Edureka
Least Squares Regression Method | Edureka
Edureka!
 
R Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
Pandas
Pandas
maikroeder
 
R Programming Language
R Programming Language
NareshKarela1
 
Data frame operations
Data frame operations
19MSS011dhanyatha
 
Data types in python
Data types in python
RaginiJain21
 
Statistics for data scientists
Statistics for data scientists
Ajay Ohri
 
Numpy
Numpy
Jyoti shukla
 
Introduction to matplotlib
Introduction to matplotlib
Piyush rai
 
Arrays in python
Arrays in python
moazamali28
 
Introduction to R and R Studio
Introduction to R and R Studio
Rupak Roy
 
R data-import, data-export
R data-import, data-export
FAO
 
Loops and functions in r
Loops and functions in r
manikanta361
 
Introduction to R
Introduction to R
Ajay Ohri
 
Data visualization using R
Data visualization using R
Ummiya Mohammedi
 
R Programming: Importing Data In R
R Programming: Importing Data In R
Rsquared Academy
 
Association rule mining.pptx
Association rule mining.pptx
maha797959
 
Least Squares Regression Method | Edureka
Least Squares Regression Method | Edureka
Edureka!
 
R Programming: Introduction To R Packages
R Programming: Introduction To R Packages
Rsquared Academy
 
Introduction to pandas
Introduction to pandas
Piyush rai
 
R Programming Language
R Programming Language
NareshKarela1
 
Data types in python
Data types in python
RaginiJain21
 
Statistics for data scientists
Statistics for data scientists
Ajay Ohri
 
Introduction to matplotlib
Introduction to matplotlib
Piyush rai
 

Viewers also liked (9)

Data 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: Introduction
Rsquared Academy
 
R Programming: Getting Help In R
R Programming: Getting Help In R
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In R
Rsquared Academy
 
R Programming: First Steps
R Programming: First Steps
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
Data 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: Introduction
Rsquared Academy
 
R Programming: Getting Help In R
R Programming: Getting Help In R
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Data Visualization Tutorial: Bar Plots
R Data Visualization Tutorial: Bar Plots
Rsquared Academy
 
R Programming: Numeric Functions In R
R Programming: Numeric Functions In R
Rsquared Academy
 
R Programming: First Steps
R Programming: First Steps
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
Rsquared Academy
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
Ad

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

Unit 1 r studio programming required.pdf
Unit 1 r studio programming required.pdf
arradhnasharma
 
R Programming
R Programming
AKSHANSH MISHRA
 
R Basics
R Basics
Dr.E.N.Sathishkumar
 
R Programing language Notes Unit 5 Data Viz in R
R Programing language Notes Unit 5 Data Viz in R
en20cs301479
 
2 data types and operators in r
2 data types and operators in r
Dr Nisha Arora
 
R Programming - part 1.pdf
R Programming - part 1.pdf
RohanBorgalli
 
FULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdf
attalurilalitha
 
7. basics
7. basics
ExternalEvents
 
Unit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data science
SyedFahad39584
 
Unit 1 financial analyticsfsddsdadsdsdsd
Unit 1 financial analyticsfsddsdadsdsdsd
bchandrasep
 
R basics
R basics
FAO
 
Introduction to R programming Language.pptx
Introduction to R programming Language.pptx
kemetex
 
Introduction To Programming In R for data analyst
Introduction To Programming In R for data analyst
ssuser26ff68
 
program to create bell curve of a random normal distribution
program to create bell curve of a random normal distribution
sonali sonavane
 
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
Edureka!
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
SreeLaya9
 
5. R basics
5. R basics
FAO
 
R
R
Acharya Biswa
 
R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)
Christopher Roach
 
Basics of R programming for analytics [Autosaved] (1).pdf
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
Unit 1 r studio programming required.pdf
Unit 1 r studio programming required.pdf
arradhnasharma
 
R Programing language Notes Unit 5 Data Viz in R
R Programing language Notes Unit 5 Data Viz in R
en20cs301479
 
2 data types and operators in r
2 data types and operators in r
Dr Nisha Arora
 
R Programming - part 1.pdf
R Programming - part 1.pdf
RohanBorgalli
 
FULL R PROGRAMMING METERIAL_2.pdf
FULL R PROGRAMMING METERIAL_2.pdf
attalurilalitha
 
Unit-5 BDS.pptx on basics of data science
Unit-5 BDS.pptx on basics of data science
SyedFahad39584
 
Unit 1 financial analyticsfsddsdadsdsdsd
Unit 1 financial analyticsfsddsdadsdsdsd
bchandrasep
 
R basics
R basics
FAO
 
Introduction to R programming Language.pptx
Introduction to R programming Language.pptx
kemetex
 
Introduction To Programming In R for data analyst
Introduction To Programming In R for data analyst
ssuser26ff68
 
program to create bell curve of a random normal distribution
program to create bell curve of a random normal distribution
sonali sonavane
 
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
R Programming For Beginners | R Language Tutorial | R Tutorial For Beginners ...
Edureka!
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
SreeLaya9
 
5. R basics
5. R basics
FAO
 
R for Pythonistas (PyData NYC 2017)
R for Pythonistas (PyData NYC 2017)
Christopher Roach
 
Basics of R programming for analytics [Autosaved] (1).pdf
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
Ad

More from Rsquared Academy (20)

Handling Date & Time in R
Handling Date & Time in R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Joining Data with dplyr
Joining Data with dplyr
Rsquared Academy
 
Explore Data using dplyr
Explore Data using dplyr
Rsquared Academy
 
Data Wrangling with dplyr
Data Wrangling with dplyr
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Introduction to tibbles
Introduction to tibbles
Rsquared Academy
 
Read 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 R
Rsquared Academy
 
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?
Rsquared Academy
 
Introduction to R
Introduction to R
Rsquared Academy
 
Data 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 Plots
Rsquared Academy
 
Data 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 Plots
Rsquared Academy
 
Data Visualization With R
Data Visualization With R
Rsquared Academy
 
R 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 R
Rsquared Academy
 
Market Basket Analysis in R
Market Basket Analysis in R
Rsquared Academy
 
Practical Introduction to Web scraping using R
Practical Introduction to Web scraping using R
Rsquared Academy
 
Writing Readable Code with Pipes
Writing Readable Code with Pipes
Rsquared Academy
 
Read 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 R
Rsquared Academy
 
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 Graphs
Rsquared Academy
 
R 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 Parameters
Rsquared Academy
 
Data Visualization With R: Learn To Modify Color Of Plots
Data Visualization With R: Learn To Modify Color Of Plots
Rsquared Academy
 
R 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 R
Rsquared Academy
 

Recently uploaded (20)

How to use _name_search() method in Odoo 18
How to use _name_search() method in Odoo 18
Celine George
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
How to Add New Item in CogMenu in Odoo 18
How to Add New Item in CogMenu in Odoo 18
Celine George
 
Wage and Salary Computation.ppt.......,x
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
2025 Completing the Pre-SET Plan Form.pptx
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
How to use _name_search() method in Odoo 18
How to use _name_search() method in Odoo 18
Celine George
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
How to Add New Item in CogMenu in Odoo 18
How to Add New Item in CogMenu in Odoo 18
Celine George
 
Wage and Salary Computation.ppt.......,x
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
2025 Completing the Pre-SET Plan Form.pptx
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 

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