Common errors and how to fix them

Day 1 - Introduction to Data Analysis with R

Selina Baldauf

Freie Universität Berlin - Theoretical Ecology

March 14, 2025

When learning a programming language, you have to be prepared to spend a lot of time fixing bugs. Don’t worry: It’s not you, it’s just how programming works! Artwork by Allison Horst

Debugging

Debugging can be annoying and we can’t avoid it, but …

  • … it’s an effective learning experience (I actually learned the most from debugging my code)

  • … it will get easier over time

  • … there are some debugging techniques to decrease the time in stages 2-7

  • … if nothing helps, there are great people all over the internet willing to help

Artwork by Allison Horst

Most common errors for beginners

and how to deal with them

Syntax errors

Example

mean(c(1,2,3)na.rm=TRUE)


How to fix

  • look for missing commas, misspelled arguments, …
  • read the error message
  • the RStudio syntax checker warns you before you run code with syntax errors
    • look for    next to line numbers in your script

Error: could not find function

Examples

select(penguins, bill_length_mm)
#> Error in select(penguins, bill_length_mm): could not find function "select"
lenght(1:10)
#> Error in lenght(1:10): could not find function "lenght"


How to fix

“Could not find function” errors have two main reasons:

  1. You forgot to load the package that the function belongs to
  • load the package using library() or call the function with packageName::functionName()
  1. You have a typo in your function call (e.g. lenght() instead of length())

Error: object not found

Example

variable_A
#> Error: object 'variable_A' not found


How to fix

  • you are trying to access an object that does not exist
    • typos in variable name (variable name is variableA but you try to access variable_A)
    • forgot to put quotes around string: print(hello) looks for a variable named hello but instead you wanted to print the string print("hello")

R crashes

Sometimes R crashes completely and you see this:

How to fix it

  • There is no fix but to start a new session

  • Make sure to save your scripts regularly!

Console prints +

R is not running code anymore and the console only prints + if you try to execute a command.

How to fix it

  • First, go to the console and hit Escape. Then you should see the > sign instead of + again.
  • Likely you forgot to close a bracket somewhere. Go to your script and check where this happened (look for   ) next to line numers

Warnings

R can give you warnings for many reasons, e.g.

  • you have NA values in your data and try to plot them
  • the function you are using is deprecated
  • the package you are using was built for another version of R

Warnings are no errors and can sometimes be ignored but:

  • make sure to read and understand warnings
  • only ignore them if you know that that’s okay, otherwise fix the underlying issue

How to troubleshoot R code

A step by step guide

Troubleshoot R: Step by step

Often, you don’t need to do all the steps but a systemmatic approach to bug fixing is very helpful.

  1. Carefully read the error message and try to fix it
  2. Is it any of the errors you learned about just now?
  3. If the error is about data or other variables: look at the structure using str()
  4. If the error is about a function: Read the documentation using ?functionName: Did you use the function correctly? Did you forget an argument?
  5. Look for answers online
  6. Ask others for help

Look for answers online

  • Search with keywords R + package name + Error message/Warning If you don’t know how do do something try searching R + package name + What you want to do, e.g.
    • “R ggplot change axis title”
    • “R sort vector”
  • Usually you can pick any of the top search results, but I recommend results from Stack Overflow
  • Always search in English to get more results

Tip

Change language of R messages to English with Sys.setenv(LANGUAGE='en')

Ask others for help

There are plenty of places where you can ask for help online. Some common and good options are:

But: You have to make sure that before, you tried all the other 5 steps.

To ask questions online, you have to learn how to ask a clear question including a reproducible example

Look here for more info on how to ask a good question about R