Creating Variables in R: Beginning R Programming Part 3

Like how the previous video only focused on the different data types you can come across, part three of the Beginning R Programming series will focus solely on variables. You’ll learn how to create variables to easily store data values, or vectors/lists of values, or datasets, or objects in R. At the end of the video, you will be able to assign values or objects to variables, understand naming style conventions, and override variable values.

A variable is a way for us
to easily store data values
or a vector or list of values
or a data set or object in R
and allows us to conveniently reference
that variable name saving us from having
to rewrite the data value, vector, or object
used many times around a program
We’ll cover vectors, main R objects and
reading in datasets later on in the
video series but we first need to
understand how to set up a
variable so that we can
write these things to a variable name
When we wrote our first
“hello world” program you might recall we
tied our data value to a
variable name called “hello.string”
So let’s revisit this again
we have a variable called “hello.string”
and we use this assignment operator
to tie whatever we want to it
such as a data value of some sort
Now our variable name should be
almost self-explanatory of what our
variable is storing or referring to
so I wouldn’t call a variable
say “cat”
and give it the value “dog” for example
it’s best just to give it a name
that kind of makes sense
Now you might be wondering
if there are naming or style
conventions for variables in R
so in R we usually
separate our one word
descriptions in our names with a period
So for example
or this could be
“animals.list”
we can also include
numbers in our variable names
So if you have like multiple data sets you
might want to call your variable “dataset1”
or if you have multiple models for example
you might want to call it “model1”
R is case-sensitive too
so if you were to capitalize “Model1”
it will not register
as we gave our variable the name “model1”
all in lowercase
you can use other
naming conventions as well
so such as underscore
or you could use, say, camel case
also a variable can only really consist of
letters, numbers, periods, underscores,
and they must begin with a letter
or a period followed by letter not a number
so for example you cannot have a
variable called “2pairs” for example
This won’t work and same goes for a
period followed by a number
you can, however, have
two pairs as in the letters “two.pairs”
and same goes for a period
and just keep in mind you also cannot use
spaces when you’re naming your variables
lastly, you can override what a variable
stores by using the same variable name
but tie it to something else
so for example “animal”
“animal <- ‘cat’ “
we’re going to override
this now with a different animal
which is “dog”
so we can see animal once belonged
to you know the the value “cat”
but we have updated it with a new value
“dog” using the same variable name
just note that once you override it
you permanently changed its value
so you would either have to rewrite it to “cat” again
or give it another variable name
if you would still like to use the “cat” value
so now that we’ve set up a variable
we can easily refer to the
variable name throughout a program so we
don’t need to keep typing out
our data values or vectors or objects
in the next video we’ll cover different operators

Prerequisites:
Download the Data Set
How to Install R

Part 4: 
Operators: Arithmetic, Rational, and Logical

Part 2:
Data Types

Full Series:
Beginning R Programming

More Data Science Material:
[Video Series] Data Mining Fundamentals
[Blog] Building Data Visualization Tools
[Blog] Data Manipulation and Exploration with dplyr

(196)

Rebecca Merrett
About The Author
- Rebecca holds a bachelor’s degree of information and media from the University of Technology Sydney and a post graduate diploma in mathematics and statistics from the University of Southern Queensland. She has a background in technical writing for games dev and has written for tech publications.

Avatar

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>