R2 Academy R Programming
Variables & Data Types
R2 AcademyCourse Material
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
➢ What is a variable?
➢ How to create a variable?
➢ How to assign value?
➢ Understand rules for naming 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 computation. Let us see how
variables can change the above scenario and help us in reusing values and computations.
R2 AcademyWhat 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 look at a simple case study to understand variables.
> 3.14 * 3 * 3
R2 AcademyCreating Variables
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
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
Value Data Type
radius 0x6d8d8c8 3 Numeric
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
Do not use names of
keywords and avoid
using the names of
Variables are case
sensitive, so average
and Average would be
Use names that are
variable names should
If name is made of
more than one word,
use underscore ( _ ) to
✓ 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:
○ Memory Address
○ Data Type
✓ Certain rules must be followed while naming variables
R2 AcademyData Types
Numeric Integer Character Logical Date/Time
1.25 1 "hello" TRUE
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:
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:
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 tests whether the variable is of type numeric. Note that while the class function returns
the data type, is.numeric returns TRUE or FALSE.
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
➢ In the last part, we coerce other data types to type integer using as.integer.
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
➢ In the last part, we coerce other data types to type character using as.character.
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.
TRUE is represented by all
numbers except 0. FALSE is
represented only by 0 and
no other numbers
We coerce other data types to type logical using as.logical.
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.
In fact, there are a couple of other ways to represent date and time.
● 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.character and
● as.data_type will coerce objects to the specified data type
○ as.character and
R2 AcademyNext Steps...
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
Visit Rsquared Academy
for tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub