R is one of the most popular languages for statistical analysis, data science, and reporting. At Dataquest, we have been adding R courses (you can learn more in our recent update). For a comparison of R and Python, check out our analysis here.

In this tutorial, we'll teach you the basics of R by building a simple grade calculator. While we do not assume any R-specific knowledge, you should be familiar with general programming concepts. You'll learn how to:

Make calculations

Store your values

Use specific functions to answer questions

This tutorial is based on part of our newly released introductory R course. The course is entirely free and includes a certificate of completion. Go here to start the course.

Calculating your grades

Let's say you're a high school senior and want to calculate your grade point average (GPA). A GPA represents the average value of the accumulated final scores earned in all your classes. You are taking seven classes, with exams, homework, and projects all equally weighted. We'll assume that the GPA is measured on a 0-100 scale.

In your math class, you've scored a 92 on exams, 87 on homework, and 85 on projects. To calculate the average math score, we could write the following:

# Math
(92 + 87 + 85)/3

We could perform tasks like calculating the average, by hand. However, if we had to calculate the averages for a thousand students, hand calculations wouldn't be an effective use of our time.

Instead, we'll use programming to ask a computer to carry out the calculations.

Performing calculations

We'll start by using R as a basic calculator. We previously wrote the following to calculate the final grade for math class:

(92 + 87 + 85)/3

This entire line of code is called an expression. We write expressions in a text file called a script. A script is a set of instructions we're giving the computer. After writing our expressions in a script, the interpreter will run the code and display the results of the expression in a new window.

Let's run the print() statement with our math score as an expression in between the ():

print((92 + 87 + 85)/3)

Running the expression, the interpreter will output the following value in a new window:

[1] 88

Note: The "[1]" will make sense when you dive deeper into vectors but you don't need to understand it for the context of this tutorial.

In our displayed result, both the calculation and the print() statement have pairs of matching parentheses. To make this clearer, here's the same calculation:

print(
(92 + 87 + 85)/3
)

Running this expression will produce the same result as writing everything on one line.

The text unexpected end of input means that the input to the R interpreter (our code) was missing a closing parentheses ). You can try playing around with the above expression and seeing what other kinds of errors you get.

Performing multiple calculations

Now that we've seen our results using the print() statement, let's dive deeper into how the R interpreter runs our code. It:

Scans and looks for syntax errors.

Interprets and runs each line of code, from top to bottom.

Exits when the last line of code is run.

We've written one expression calculating your final score in math class. To understand the sequential way R code is interpreted, let's also add an expression calculating your chemistry score. In Chemistry, your scores were 90, 81, and 92.

What happens if we run both calculations on separate lines?

print((92 + 87 + 85)/3)
print((90 + 81 + 92)/3)

Running this code, the R interpreter will display:

[1] 88
[1] 87.66667

Does R always display two lines if we write two lines of code? What if we break up our code into multiple lines?

print(
(92 + 87 + 85)/3
)
print(
(90 + 81 + 92)/4
)

The R interpreter will still display the same values:

[1] 88
[1] 87.66667

Notice how R interprets our code. Each print statement corresponds to it's own line in the result:

If we wanted to calculate the average scores for writing and art, we can write these expressions on each subsequent line:

Performing calculations using arithmetic operators

+ and / are called arithmetic operators. Arithmetic operators are used to carry out mathematical operations. In the following diagram, you'll find a list of the most common operators and a simple expression using each operator:

For those who are unfamiliar with exponentiation, exponentiation is a way of multiplying a number by itself a specific number of times, using the ** or ^ operator. If we wanted to multiply the value 4 by itself 3 times, this would look like the following using the multiplication * operator:

4 * 4 * 4

While multiplying 4 by itself three times using the multiplication operator isn't too cumbersome, if we wanted to multiply the value 4 by itself 20 times, using the multiplication operator isn't the most efficient method. Instead, we can express the calculation as an exponent:

4**20

Running 4**20 will return:

[1] 1.099512e+12

Now that we understand arithmetic operators, let's calculate the final scores for our last three classes: history, music and physical education:

Performing Calculations with Order of Operations

Now that we've learned how to use arithmetic operators to calculate the average scores for each class, let's return to our average calculation for math:

print(
(92 + 87 + 85)/3
)

What if we deleted the parenthesis surrounding 92 + 87 + 85?

print(
92 + 87 + 85/3
)

This will display:

207.333

By deleting the parentheses surrounding 92 + 87 + 85, the R interpreter makes a different calculation. When using multiple operators, there are rules that determine the order in which calculations are performed.

A simple way to determine the order of your calculations, is to throw a parenthesis around the calculation you want performed first. This is useful for a more complex calculation like this: