Lesson 1: Your First Steps in R

Use R for arithmetic, store values in variables, and calculate a mean.

1 Learning goals

By the end of this lesson, you will be able to:

  • Run a calculation in the RStudio Console.
  • Store a value in a variable.
  • Create a numeric vector and calculate its mean.

All examples use base R; no additional R packages are required to run them.

2 Use R as a calculator

Type the following expression into the RStudio Console and press Enter:

2 + 2
[1] 4

The output is 4. R uses * for multiplication and / for division.

10 * 5
[1] 50
20 / 4
[1] 5

3 Store a value

Use <- to assign a value to a variable. You can then use its name in later calculations.

number_of_students <- 20
number_of_students + 5
[1] 25

This calculation returns 25. The stored value remains 20 because we did not assign a new value to number_of_students.

4 Calculate a mean

The function c() combines values into a vector. Here we use invented practice scores.

scores <- c(6, 8, 10, 12, 14)
mean(scores)
[1] 10

The mean is 10: the sum of the five scores is 50, and 50 / 5 = 10.

5 Try it yourself

Create a vector containing 5, 10, and 15, then calculate its mean.

practice_values <- c(5, 10, 15)
mean(practice_values)
[1] 10

The answer is 10.

6 Download and practise

Return to the course home