Instructions

This document will take you through some tips and tricks to be able to work independently in R. It covers Error messages, Warning messages, and General help.

Questions appear in blue. Answers appear in red.


By the end of these exercises, you should be able to:

Dealing with Errors

Error messages are quite common in R. If you stuck with the default colour scheme for RStudio, they will appear in red in the R console window.

Example of an Error

Example of an Error

This can be a bit intimidating or just frustrating after you have carefully written your code. But they can also be helpful, yes, really. Most errors in R, will actually tell you what is wrong with your code (some won’t and we will get to those), you just need to know what to look for.

How to read an error in R

To illustrate this section we will focus on three very common errors and one less common:

Error 1

Error 1


Error 2

Error 2


Error 3

Error 3


Error 4

Error 4

There are a couple of steps that can be taken to solve these errors, we will go through them below.

Step 1: read the error

The first thing to do is to read the errors.

Sometimes the error itself will tell us what has gone wrong.

Read all of the errors above, what do you think has gone wrong with the line of code?

I had a go, now show me the answers.

Error 1: tells us that there is an error in the call of function mean() of object x, R cannot find object x. This means that R cannot find the object x, it does not exist yet in the environment.

Error 2: tells us that there is an error in the call of function Sd() of vector c(1, 2, 3, 4, 5), R cannot find function Sd(). This means that R cannot find the function Sd(), it does not exist in any packages loaded in R, nor has a user written it.

Error 3: tells us that there is an unexpected symbol in the line of code, something R is not expecting to be there. This means that something is wrong in the line of code, there is either an extra symbol or one missing.

Error 4: tells us there is an error in what looks like a random string of symbols and there is a subscript that is out of bounds…. This is not very interpretable at the moment.

Step 2: try to fix the error

Now you hopefully have a rough idea what the errors mean, we need to fix them in order to get our code working again. There are many ways this can be done but we will go through a few examples.

We will solve each of the example errors in turn to demonstrate how this can be done.

You can have a go first and then look at the answers.

I had a go, now show me the answers.

Error 1

When reading this error we can see that R cannot find the object x. This means that x does not exist in your R session. If you look at the Environment panel to the top right, it will not be shown.

There are two main ways this can happen:

  • First, you did not yet create the object x. If you create it e.g.  x <- c(1,2,3) trying to take the mean, the line should work.

  • Second, there could be a typo. Maybe you think you did already make object x but actually you made an object with a capital x (X). This time correcting the typo e.g. mean(X) would fix the line.

Error 2

When reading this error we can see that R this time cannot find the function Sd. This means that Sd does not exist as a function in your R session.

There are three main ways this can happen:

  • First, there could be a typo. In this case you want to take the standard deviation of the set of numbers. To check what the correct function name is you can Google take standard deviation in R. If I do this I get the following results: