SlideShare a Scribd company logo
1
Introduction
In this module, we will learn about variables & data types. Specifically:
what is a variable?
how to create variables in R?
how to use variables created?
components of a variable
naming conventions for a variable
data types
numeric/double
integer
logical
character
date/time
•
•
•
•
•
•
•
•
•
•
•
2
3
What is a variable?
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 use a simple case study to understand variables. 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 analysis or computation. Let us see how
variables can change the above scenario and help us in reusing values and computations.
•
•
•
3.14 * 3 * 3
## [1] 28.26
4
Creating Variables
A variable consists of 3 components:
variable name
assignment operator
variable value
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 using the assignment operator <-.
Now that we have learnt to create variables, let us see how we can use them for other computations. For our
case study, we will use the radius variable to compute the area of a circle.
•
•
•
radius <- 3
radius
## [1] 3
5
Using Variables
We will create two variables, radius and pi, and use them to compute the area of a circle and store it in
another variable area.
# assign value 3 to variable radius
radius <- 3
# assign value 3.14 to variable pi
pi <- 3.14
# compute area of circle
area <- pi * radius * radius
# call radius
radius
## [1] 3
# call area
area
6
7
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 the names of keywords and avoid using the names of built in functions.
Variables are case sensitive; average and Average would be different variables.
Use names that are descriptive. Generally, variable names should be nouns.
If the name is made of more than one word, use underscore to separate the words.
•
•
•
•
•
•
8
9
Numeric
In R, numbers are represented by the data type numeric. We will first create a variable and assign it a value.
Next we will learn a few methods of checking the type of the variable.
# create two variables
number1 <- 3.5
number2 <- 3
# check data type
class(number1)
## [1] "numeric"
class(number2)
## [1] "numeric"
# check if data type is numeric
is.numeric(number1)
## [1] TRUE
is.numeric(number2)
## [1] TRUE
10
Numeric
If you carefully observe, integers are also treated as numeric/double. We will learn to create integers in a
while. In the meanwhile, we have introduced two new functions in the above example:
class(): returns the class or type
is.numeric(): tests whether the variable is of type numeric
•
•
11
Integer
Unless specified otherwise, integers are treated as numeric or double. In this section, we will learn to create
variables of the type integer and to convert other data types to integer.
create a variable number1 and assign it the value 3
check the data type of number1 using class
create a second variable number2 using as.integer() and assign it the value 3
check the data type of number2 using class
finally use is.integer() to check the data type of both number1 and number2
•
•
•
•
•
12
Integer
# create a variable and assign it an integer value
number1 <- 3
# create another variable using as.integer
number2 <- as.integer(3)
# check the data type
class(number1)
## [1] "numeric"
class(number2)
## [1] "integer"
# use is.integer to check data type
is.integer(number1)
## [1] FALSE
is.integer(number2)
## [1] TRUE
13
Character
Letters, words and group of words are represented by the data 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. Let us create two variables to store the first and last name of a some random guy.
# first name
first_name <- "jovial"
# last name
last_name <- 'mann'
# check data type
class(first_name)
## [1] "character"
class(last_name)
## [1] "character"
# use is.charactert to check data type
is.character(first_name)
## [1] TRUE
is.character(last_name)
## [1] TRUE
14
Integer
You can coerce any data type to character using as.character().
# create variable of different data types
age <- as.integer(30) # integer
score <- 9.8 # numeric/double
opt_course <- TRUE # logical
today <- Sys.time() # date time
as.character(age)
## [1] "30"
as.character(score)
## [1] "9.8"
as.character(opt_course)
## [1] "TRUE"
as.character(today)
## [1] "2019-03-03 09:24:28"
15
Logical
Logical data types take only 2 values. Either TRUE or FALSE. Such data types are created when we compare
two objects in R using comparison or logical operators.
create two variables x and y
assign them the values TRUE and FALSE respectively
use is.logical() to check data type
use as.logical() to coerce other data types to logical
•
•
•
•
# create variables x and y
x <- TRUE
y <- FALSE
# check data type
class(x)
## [1] "logical"
is.logical(y)
## [1] TRUE
16
Logical
The outcome of comparison operators is always logical. In the below example, we compare two numbers to
see the outcome.
# create two numeric variables
x <- 3
y <- 4
# compare x and y
x > y
## [1] FALSE
x < y
## [1] TRUE
# store the result
z <- x > y
class(z)
## [1] "logical"
17
Logical
TRUE is represented by all numbers except 0. FALSE is represented only by 0 and no other numbers.
# TRUE and FALSE are represented by 1 and 0
as.logical(1)
## [1] TRUE
as.logical(0)
## [1] FALSE
# using numbers
as.numeric(TRUE)
## [1] 1
as.numeric(FALSE)
## [1] 0
# using different numbers
as.logical(-2, -1.5, -1, 0, 1, 2)
## [1] TRUE
18
Logical
Use as.logical() to coerce other data types to logical.
# create variable of different data types
age <- as.integer(30) # integer
score <- 9.8 # numeric/double
opt_course <- TRUE # logical
today <- Sys.time() # date time
as.logical(age)
## [1] TRUE
as.logical(score)
## [1] TRUE
as.logical(opt_course)
## [1] TRUE
as.logical(today)
## [1] TRUE
19
Summary
Variables are the building blocks of a programming language
Variables reference memory locations that store information/data
An assignment operator <- assigns values to a variable
A variable has the following components:
name
memory address
value
data type
Certain rules must be followed while naming variables
•
•
•
•
•
•
•
•
•
20
Summary
numeric, integer, character, logical and date are the basic data types in R
class() or typeof() return the data type
is.data_type checks whether the data is of the specified data type
is.numeric()
is.integer()
is.character()
is.logical()
is.date()
as.data_type will coerce objects to the specified data type
as.numeric()
as.integer()
as.character()
as.logical()
as.date()
•
•
•
•
•
•
•
•
•
•
•
•
•
•
21
22

More Related Content

What's hot

Back patching
Back patchingBack patching
Back patching
santhiya thavanthi
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
krishna singh
 
How to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | EdurekaHow to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | Edureka
Edureka!
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
Esmita Gupta
 
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
BIT Durg
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
Mohd Sajjad
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
eman lotfy
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
Ravinder Kamboj
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
Arockia Abins
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 

What's hot (20)

Back patching
Back patchingBack patching
Back patching
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
How to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | EdurekaHow to use Map() Filter() and Reduce() functions in Python | Edureka
How to use Map() Filter() and Reduce() functions in Python | Edureka
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
List in Python
List in PythonList in Python
List in Python
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
C++ classes
C++ classesC++ classes
C++ classes
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
 
Loops in Python
Loops in PythonLoops in Python
Loops in Python
 
Python Modules
Python ModulesPython Modules
Python Modules
 

Similar to Variables & Data Types in R

Python
PythonPython
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
ARUN DN
 
python.pdf
python.pdfpython.pdf
python.pdf
wekarep985
 
R Programming
R ProgrammingR Programming
R Programming
AKSHANSH MISHRA
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
Data Types of R.pptx
Data Types of R.pptxData Types of R.pptx
Data Types of R.pptx
Ramakrishna Reddy Bijjam
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
Rupak Roy
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with N
OllieShoresna
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 
R data types
R   data typesR   data types
R data types
Learnbay Datascience
 
Array assignment
Array assignmentArray assignment
Array assignment
Ahmad Kamal
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
GaneshRaghu4
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
AnirudhaGaikwad4
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
SreeLaya9
 
Token and operators
Token and operatorsToken and operators
Token and operators
Samsil Arefin
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
Mr. Vikram Singh Slathia
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
GaneshRaghu4
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
MariappanR3
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
MdMahediHasan54
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
z9819898203
 

Similar to Variables & Data Types in R (20)

Python
PythonPython
Python
 
Ggplot2 work
Ggplot2 workGgplot2 work
Ggplot2 work
 
python.pdf
python.pdfpython.pdf
python.pdf
 
R Programming
R ProgrammingR Programming
R Programming
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
Data Types of R.pptx
Data Types of R.pptxData Types of R.pptx
Data Types of R.pptx
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
Data Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with NData Manipulation with Numpy and Pandas in PythonStarting with N
Data Manipulation with Numpy and Pandas in PythonStarting with N
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
R data types
R   data typesR   data types
R data types
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
 
Unit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptxUnit I - 1R introduction to R program.pptx
Unit I - 1R introduction to R program.pptx
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 

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
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
Rsquared Academy
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
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: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
Rsquared Academy
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
Rsquared Academy
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
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
 

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
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
R Markdown Tutorial For Beginners
R Markdown Tutorial For BeginnersR Markdown Tutorial For Beginners
R Markdown Tutorial For Beginners
 
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: Introduction to Matrices
R Programming: Introduction to MatricesR Programming: Introduction to Matrices
R Programming: Introduction to Matrices
 
R Programming: Introduction to Vectors
R Programming: Introduction to VectorsR Programming: Introduction to Vectors
R Programming: Introduction to Vectors
 
R Programming: Variables & Data Types
R Programming: Variables & Data TypesR Programming: Variables & Data Types
R Programming: Variables & Data Types
 
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
 

Recently uploaded

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
Roger Valdez
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 

Recently uploaded (20)

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
Everything you wanted to know about LIHTC
Everything you wanted to know about LIHTCEverything you wanted to know about LIHTC
Everything you wanted to know about LIHTC
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 

Variables & Data Types in R

  • 1. 1
  • 2. Introduction In this module, we will learn about variables & data types. Specifically: what is a variable? how to create variables in R? how to use variables created? components of a variable naming conventions for a variable data types numeric/double integer logical character date/time • • • • • • • • • • • 2
  • 3. 3
  • 4. What is a variable? 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 use a simple case study to understand variables. 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 analysis or computation. Let us see how variables can change the above scenario and help us in reusing values and computations. • • • 3.14 * 3 * 3 ## [1] 28.26 4
  • 5. Creating Variables A variable consists of 3 components: variable name assignment operator variable value 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 using the assignment operator <-. Now that we have learnt to create variables, let us see how we can use them for other computations. For our case study, we will use the radius variable to compute the area of a circle. • • • radius <- 3 radius ## [1] 3 5
  • 6. Using Variables We will create two variables, radius and pi, and use them to compute the area of a circle and store it in another variable area. # assign value 3 to variable radius radius <- 3 # assign value 3.14 to variable pi pi <- 3.14 # compute area of circle area <- pi * radius * radius # call radius radius ## [1] 3 # call area area 6
  • 7. 7
  • 8. 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 the names of keywords and avoid using the names of built in functions. Variables are case sensitive; average and Average would be different variables. Use names that are descriptive. Generally, variable names should be nouns. If the name is made of more than one word, use underscore to separate the words. • • • • • • 8
  • 9. 9
  • 10. Numeric In R, numbers are represented by the data type numeric. We will first create a variable and assign it a value. Next we will learn a few methods of checking the type of the variable. # create two variables number1 <- 3.5 number2 <- 3 # check data type class(number1) ## [1] "numeric" class(number2) ## [1] "numeric" # check if data type is numeric is.numeric(number1) ## [1] TRUE is.numeric(number2) ## [1] TRUE 10
  • 11. Numeric If you carefully observe, integers are also treated as numeric/double. We will learn to create integers in a while. In the meanwhile, we have introduced two new functions in the above example: class(): returns the class or type is.numeric(): tests whether the variable is of type numeric • • 11
  • 12. Integer Unless specified otherwise, integers are treated as numeric or double. In this section, we will learn to create variables of the type integer and to convert other data types to integer. create a variable number1 and assign it the value 3 check the data type of number1 using class create a second variable number2 using as.integer() and assign it the value 3 check the data type of number2 using class finally use is.integer() to check the data type of both number1 and number2 • • • • • 12
  • 13. Integer # create a variable and assign it an integer value number1 <- 3 # create another variable using as.integer number2 <- as.integer(3) # check the data type class(number1) ## [1] "numeric" class(number2) ## [1] "integer" # use is.integer to check data type is.integer(number1) ## [1] FALSE is.integer(number2) ## [1] TRUE 13
  • 14. Character Letters, words and group of words are represented by the data 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. Let us create two variables to store the first and last name of a some random guy. # first name first_name <- "jovial" # last name last_name <- 'mann' # check data type class(first_name) ## [1] "character" class(last_name) ## [1] "character" # use is.charactert to check data type is.character(first_name) ## [1] TRUE is.character(last_name) ## [1] TRUE 14
  • 15. Integer You can coerce any data type to character using as.character(). # create variable of different data types age <- as.integer(30) # integer score <- 9.8 # numeric/double opt_course <- TRUE # logical today <- Sys.time() # date time as.character(age) ## [1] "30" as.character(score) ## [1] "9.8" as.character(opt_course) ## [1] "TRUE" as.character(today) ## [1] "2019-03-03 09:24:28" 15
  • 16. Logical Logical data types take only 2 values. Either TRUE or FALSE. Such data types are created when we compare two objects in R using comparison or logical operators. create two variables x and y assign them the values TRUE and FALSE respectively use is.logical() to check data type use as.logical() to coerce other data types to logical • • • • # create variables x and y x <- TRUE y <- FALSE # check data type class(x) ## [1] "logical" is.logical(y) ## [1] TRUE 16
  • 17. Logical The outcome of comparison operators is always logical. In the below example, we compare two numbers to see the outcome. # create two numeric variables x <- 3 y <- 4 # compare x and y x > y ## [1] FALSE x < y ## [1] TRUE # store the result z <- x > y class(z) ## [1] "logical" 17
  • 18. Logical TRUE is represented by all numbers except 0. FALSE is represented only by 0 and no other numbers. # TRUE and FALSE are represented by 1 and 0 as.logical(1) ## [1] TRUE as.logical(0) ## [1] FALSE # using numbers as.numeric(TRUE) ## [1] 1 as.numeric(FALSE) ## [1] 0 # using different numbers as.logical(-2, -1.5, -1, 0, 1, 2) ## [1] TRUE 18
  • 19. Logical Use as.logical() to coerce other data types to logical. # create variable of different data types age <- as.integer(30) # integer score <- 9.8 # numeric/double opt_course <- TRUE # logical today <- Sys.time() # date time as.logical(age) ## [1] TRUE as.logical(score) ## [1] TRUE as.logical(opt_course) ## [1] TRUE as.logical(today) ## [1] TRUE 19
  • 20. Summary Variables are the building blocks of a programming language Variables reference memory locations that store information/data An assignment operator <- assigns values to a variable A variable has the following components: name memory address value data type Certain rules must be followed while naming variables • • • • • • • • • 20
  • 21. Summary numeric, integer, character, logical and date are the basic data types in R class() or typeof() return the data type is.data_type checks whether the data is of the specified data type is.numeric() is.integer() is.character() is.logical() is.date() as.data_type will coerce objects to the specified data type as.numeric() as.integer() as.character() as.logical() as.date() • • • • • • • • • • • • • • 21
  • 22. 22