3  Variables in R

Practise this interactively in Course 1 (Introduction to R), lesson 2

Learning Objectives: After this chapter you will be able to:

  • Explain what a variable is and why programs use them.
  • Create and reassign variables with <-.
  • Use variables in arithmetic and in function calls.
  • Name variables legally, including the rule that permits a leading ..
  • Choose snake_case over periods, which are legal but discouraged.

3.1 Introduction

In the previous chapter, we learned to install RStudio. In this chapter, we will learn about variables. You can skip this chapter if you have prior experience in another programming language.

3.2 What is a variable?

  • variables are the fundamental elements of any programming language
  • they represent values that are likely to change
  • they reference memory locations that store data

Let us use a simple case study to understand 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:

3.14 * 3 * 3
[1] 28.26

Every new analysis or computation would need the radius and the area typed again. Let us see how variables help us reuse values and computations.

3.3 Creating Variables

A variable consists of 3 components:

  • variable name
  • assignment operator
  • variable value

We can store the radius by assigning its value to a variable. In this case, we create a variable called radius and assign it the value 3 using the assignment operator <-.

radius <- 3
radius
[1] 3

Now that we have learned to create variables, let us see how we can use them for other computations. For our case study, we will use the radius variable to compute the area of a circle.

3.4 Using Variables

We will use radius and R’s built-in constant pi to compute the area of a circle and store it in another variable area. R already provides pi; there is no need to assign it a value.

# assign value 3 to variable radius
radius <- 3

# compute area of circle
area <- pi * radius^2

# call radius
radius
[1] 3
# call area 
area
[1] 28.27433

3.5 Components of a Variable

Diagram of the components of a variable in R

3.6 Naming Conventions

  • A name must begin with a letter or a dot (.); e.g. .hidden <- 42 is valid in base R. Do not use numbers, dollar sign ($) or underscore (_) as the first character.
  • The name can contain numbers or underscores. Do not use dash (-). Periods (.) are syntactically valid in R variable names, but are discouraged in modern style guides (tidyverse style prefers snake_case).
  • Do not use the names of keywords and avoid using the names of built-in functions.
  • Variables are case sensitive; average and Average would be different variables.
  • Use descriptive names. Generally, variable names should be nouns.
  • If the name contains more than one word, use underscores to separate the words.

Key points

  • Use <- to assign a value to a variable and the variable name to reuse it.
  • Changing a variable lets you repeat a calculation with a new value.
  • R already provides the constant pi.
  • Variable names are case sensitive; choose descriptive names such as snake_case.

Next: Data Types in R

Practise interactively: Course 1, lesson 2