R Variables
Oct 07, 2023
DataSciencePursuit
Variables are storage locations for data in computer memory. The variable name can be used to retrieve, reuse, and change the data stored.
We will look at some numeric examples. However, keep in mind that variables apply to data of different types, but we’ll continue to work with numbers for now until we learn about other data types.
Variable Assignment
To create a variable, we use the assignment operator. The syntax is as follows:
variable name <- data
To the left, you have whatever variable name you choose. In the middle, there is the assignment operator (the arrow symbol is made up of the less than sign and the minus sign). And, to the right, the value or data you want to store.
Let’s create a variable for the current year which is 2023 and we will simply name it year.
year <- 2023
Notice that we do not get any output, error, or otherwise. But if you look in the Environment pane, you will see the variable and its value as in the image below:

This is one way you can know that variable assignment worked. Another way is to retrieve the variable by typing its name as below.
year
[1] 2023
The variable year can now be used like the number 2023. For example, to calculate what year it will be in 3 years, writing 2023 + 3 is the same as writing year + 3, both will give us 2026.
year + 3
[1] 2026
This applies to all the operators we learnt in the previous lesson.
Case sensitivity and code completion
R is case-sensitive. If we try to call the variable Year (upper-case Y)
Year
Error: object 'Year' not found
This error means that the variable Year does not exist (this error will also occur if the variable name is misspelled).
RStudio code completion to the rescue. If you type ‘Yea’, you will get a pop-up with the suggested variable names from the Environment pane.

year
[1] 2023
Code completion needs a few letters to start working, these letters need to have the correct spelling. If code completion is not working, it’s best to check the environment pane or the code for the correct spelling.
Variable Naming
There are some things to consider when naming variables.
Syntax
Besides the syntax for variable assignment (above), there is syntax for variable naming as well:
-
A variable’s name can only include letters, numbers, full stop or period ( . ) and underscore ( _ ).
-
Can not include spaces
-
Can not include any other special characters
-
-
A variable’s name can not start with a number or an underscore.
- A number cannot be a variable name
-
If a variable name starts with a period, the second character cannot be a number. (I do not name my variables starting a period, partially because these variables do not show in the environment pane.)
If these rules are violated, R will give an error. Here are a few examples:
If we wanted to be more descriptive, we could name our variable current year instead of just year.
# using a space
current year <- 2023
Error: unexpected symbol in "current year"
There are multiple solutions to this:
- Use period ( . ) in place of spaces with periods (most common solution in R)
- Use underscore instead of spaces (common in Python and other programming languages)
- Write the words as one word (no spaces).
# using period instead of spaces
current.year <- 2023
# starting with a number
2023year <- 2023
Error: unexpected symbol in "2023year"
# starting with a period, then a number
.2023year <- 2023
Error: unexpected symbol in ".2023year"
Working examples
# working example with a number
year2023 <- 2023
General naming recommendations
It is recommended to:
-
use descriptive but short names. This will help your future self and other people looking at the code to more quickly understand what the variable is doing.
-
avoid using reserved R names. We will learn some of these in this course, for example, function names. This is a recommendation because it will not create errors, but it may cause some confusion when reading the code.
Updating Variables
We’ll create another variable named num_oranges (number of oranges).
Tommy has 20 oranges, he gave 5 to his friends. How many oranges does Tommy have now?
# start with the initial number of oranges
num_oranges <- 20
num_oranges
[1] 20
We can either:
- Reassign our variable with the number 15, which will overwrite or replace the current value.
num_oranges <- 15
num_oranges
[1] 15
- We can subtract 5 from num_oranges and assign it back to num_oranges, all in 1 line.
num_oranges <- num_oranges - 5
num_oranges
[1] 10
R first evaluates what is to the right of the assignment operator to get 10, then it assigns that to num_oranges, replacing the original value.
The second method is preferred as it is less manual. This will come in handy down the line when we have more complex and longer code.
How long do variables last?
R automatically keeps your variables until you either delete your variables for example by clearing the environment pane using the little broom icon, or you quit/close R. When you close R, it asks you to save the workspace image. If you save it, you save all your variables as well, the next time you open R, you will see your variables. If not, they will all be cleared.