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
write these things to a variable name
“hello world” program you might recall we
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
and give it the value “dog” for example
it’s best just to give it a name
Now you might be wondering
if there are naming or style
conventions for variables in R
descriptions in our names with a period
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”
so if you were to capitalize “Model1”
as we gave our variable the name “model1”
naming conventions as well
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
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
this now with a different animal
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
(420)