Created by Gergana - last updated 4th April 2019 by Sandra

Tutorial aims:

  1. Learn how to pick up on errors in R
  2. Get familiar with common errors and solutions
  3. Learn how to find help online
  4. Practice by fixing errors in an example script

In our first tutorial, we learned how to import data into RStudio, conduct a simple analysis (calculate species richness) and plot the results. Not bad for a first time! If you need reminding of some programming terms, take a quick look at our glossary.

Programming comes with a learning curve and you will no doubt encounter many error messages while you familiarise yourself with the workings of R. But fear not! Today we will tackle some of the most common coding errors and help you avoid them. You might have seen some of these error messages already, but after completing this tutorial, we hope they won’t appear too often on your RStudio screens.

1. Learn how to pick up on errors in R

In addition to keeping a record of your code, scripts are also useful for detecting simple coding errors before you’ve even run the code. If RStudio picks up on a character missing, a command that doesn’t make sense due to spelling errors or similar, a little red x appears next to that line of code. Scanning your code for x’s before running it is always a good idea and it’s very convenient since you know exactly on which line you made a mistake. The other way R reports errors is through messages in the console, which appear after you run code that is not quite right. Although the error messages look scary (the red font and words like “fatal” sure give them a bad reputation), they are actually the second best option to no errors at all: R has identified there is a problem and from the message, you can figure out what it is and solve it!

RStudio GUI error and code linter warning, annotated

2. Get familiar with common errors and solutions

Here we have compiled a list of mistakes we often make. Do you think we have forgotten an error message or problem you encounter often? Please let us know at ourcodingclub(at) and we will add it to our list!

If you have loaded several similar packages from your library, they might contain different functions with the same name and your code might break if R is confused as to which one to use - running package::function, e.g. dplyr::filter will return information on the function in the console. Note that R will try to add () at the end of dplyr::filter. Delete them and run the code.

If you are reading up on R online, or copying and modifying code, you might be using a function from a new package without knowing. If it looks unfamiliar, googling its name with “r package” might reveal its origin. Sometimes packages depend on other packages to run. Often those get installed automatically when you install the package, but sometimes you get an error message asking you to install another package, easily solved by install.packages("newpackage").

Incorrect stacked bar chart

Figure 1. An unfortunate looking barplot! The data were chosen to be a data matrix, but, because in matrices all variables are of the same type, R expects taxa_f - the names of the different taxa - to have a numerical value, and lumps all the species richness values together in the second bar. A data frame was definitely a better choice!

Aborted R Session error message

RStudio console prompt changed to "plus" syrbols

Cheat sheet!

Here are some of the most common error messages you’ll meet when you get started, and what they actually mean:

3. Learn how to find help

An online search for the error message (along with “R” and the function or package name) is always a good start. Chances are someone has already encountered that error and has asked about it online. If the error message is very long, try paraphrasing based on what you think the problem might be (and delete references to your specific objects and file names). There are several really useful online forums and websites where people ask for and receive help, such as Stackoverflow and Rbloggers.

For “how to …” type queries, a search will often result in tutorials, and even Youtube videos as well. For instance, “ ggplot how to change axis titles” or “dplyr filtering on two conditions” will quickly get you what you need.

R Help Of course, R has built-in documentation for all functions and packages. We already mentioned that you can type help() or ?function-name (try ??function-namefor a broader search). The RDocumentation website contains much of the same in a slightly nicer format, with examples and more links.

We have also compiled a “Useful links” list of helpful websites and tutorials where you can find additional help.

Of course, R won’t always tell you if you are doing something wrong: sometimes your code is correct, but you are doing the wrong type of analysis for your data. Nevertheless, making sure you avoid easy-to-make mistakes is a great place to start - even the fanciest, super-advanced tests can be brought down by a single missing comma.

A warning on warnings!

Errors are not the only scary red text that will appear in the console. Often, you will run code and get a message saying something like ‘There were 13 warnings, use warnings()to see them’. It could also be a specific warning telling you that R is ignoring NAs (missing) data in a plot, or that a function generated unexpected results such as NaN (not a number).

Warnings are not necessarily the end of the world, but you should see them as major red flags, and make sure you know what is going on before you pursue your analysis. Like error messages, a simple online search of the warning text will often put you on the right track.

4. Practice!

Practice truly is the best way to learn how to avoid errors in R - to get you started, we have written a purposefully wrong script - you can download the file from this Github repository. There you will find the data edidiv.csv, as well as the wrong (CC_2_RBasics_Wrong_Script.R) and right script. Can you fix all the mistakes?

Tutorial outcomes:

  1. You know how R reports errors, both in script files and in the console
  2. You can solve common mistakes in R
  3. If you can’t figure out a solution yourself, you know where to find help

Next up. we have a tutorial on Coding Etiquette! Developing code-writing routines and sticking to defined conventions is a good way to ensure uniform, mistake-free code that runs smoothly!

Feeling ready to go one step further? Learn how to format and manipulate data in a tidy and efficient way with our tidyr and dplyr tutorial! Keen to make more graphs? Check out our data visualisation tutorial.

Stats from Scratch stream

Doing this tutorial as part of our Data Science for Ecologists and Environmental Scientists online course?

This tutorial is part of the Stats from Scratch stream from our online course. Go to the stream page to find out about the other tutorials part of this stream!

If you have already signed up for our course and you are ready to take the quiz, go to our quiz centre. Note that you need to sign up first before you can take the quiz. If you haven't heard about the course before and want to learn more about it, check out the course page.

Stay up to date and learn about our newest resources by following us on Twitter!
Contact us with any questions on

Related tutorials: