Skip to main content Link Menu Expand (external link) Left Arrow Right Arrow Document Search Copy Copied

Lesson 2 - Variables

How do we store data that we can use and modify? Using variables!

Table of Contents

Lesson Objectives

  • Use variables to store and modify data.

Identifiers

Identifiers are variable names. When you create, use, or modify a variable, you refer to the variable by its identifier.

R has some rules and restrictions for identifiers.

  • Identifiers can consist of letters (uppercase and lowercase), digits, periods (.), and underscores (_).
  • All identifiers must start with a letter or with a period (.).
    • If the identifier starts with a period, it cannot be followed by a digit.
  • There are some identifiers reserved for R’s use only; they’re usually common words like “if”, “else”, “repeat”, etc.
  • Identifiers are case-sensitive.
    • myVariable and MYVARIABLE are considered two different variables.

Assigning Values to Variables

The format to create a variable is:

identifier <- value

Suppose we wanted to assign myVariable the value of 10. We would do

myVariable <- 10

The line above is read as “myVariable is assigned the value of 10.”

If we decide later on that we want to reassign the value of myVariable to something else, we would again do

myVariable <- <new value>

and that would set the value of myVariable to our new value.

Using Variables in Math

If we want to use a variable in our math expression, we just reference the variable using its identifier.

5 * myVariable

and it outputs the answer.

Finding All Defined Variables

Aside from using the Environment tab in RStudio, we can also use the ls() function to get a list of all our defined variables.

Input

a <- 5
b <- 2
myVariable <- 2.5
.myVariable <- 2.6

ls()

Output

[1] "a"          "b"          "myVariable"

You’ll notice that any variables with an identifier that starts with a period (.) are not shown. If you want to show them as part of the output, you have to define all.name = TRUE inside the ls() function.

Input

a <- 5
b <- 2
myVariable <- 2.5
.myVariable <- 2.6

ls(all.name <- TRUE)

Output

[1] ".myVariable"          "a"          "b"          "myVariable"

Deleting a Variable

If you wanted to delete a variable, you can use the rm() function. Usually, deleting a variable is done to prevent accidental usage of it, but it’s not required or done often.

myVariable <- 5
rm(myVariable)

Writing Comments in your Code

Sometimes, it’s useful to write yourself (or others) comments about your code. They can be used to explain pieces of code, and generally, they make the code more readable.

R considers everything after a # symbol a comment and ignores it when executing the code.

Input

# sqrt() is the square root function
sqrt(4)
sqrt(16)

Output

[1] 2
[1] 4

Since R ignores comments, you can also use it to prevent lines of code from being executed. This is particularly useful when troubleshooting issues with your code.

Input

# sqrt() is the square root function
# sqrt(4)
sqrt(16)

Output

[1] 4

Key Points / Summary

  • You can use variables to store data.