In this tutorial, we learn to create variables in R. Followed by that, we explore the different data types including numeric, integer, character, logical and date/time.
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.
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.
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.
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.
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