Error Handling and Debugging

Learn to read R’s error messages and fix the most common beginner mistakes.

Start here: how to read an error

Getting errors is normal — every R user, beginner or expert, sees them constantly. An error is not a sign you did something wrong; it is R telling you exactly where it got stuck. The skill worth learning is not avoiding errors, but reading them.

When something goes wrong, R stops and prints a message that starts with Error. Run the chunk below and look at the message it produces:

You will see something like:

Error in `mutate()`:
ℹ In argument: `weight_g = wieght_mg/1000`
Caused by error:
! object 'wieght_mg' not found

Read it from the top down:

  • Which function failedmutate().
  • Which part — the argument weight_g = wieght_mg/1000.
  • The actual cause — the last line: object 'wieght_mg' not found.

The last line is usually the most useful: wieght_mg is a typo for weight_mg. Fix the spelling and the error disappears:

TipThe one habit that fixes most errors

Read the message top to bottom, then focus on the line that names a missing object, function, or argument. That line almost always points straight at the fix.

How to use this page. Each card below gives you a small broken program. Run it, read the error, and try to fix it yourself. Then open Show solution to check your fix and see why it works. Work through one bug at a time — run it, fix it, re-run.

Syntax and typing errors

These are the mechanical mistakes: a wrong name, a missing comma, a stray space. They are the most common errors and the quickest to fix once you recognise them.

1) Typos in names

R is case-sensitive: data and Data are two different names.

Show solution

Why: You created data (lowercase), but asked for Data (capital D). R never created Data, so it reports object 'Data' not found. Match the name exactly.


2) = vs == in conditions

A single = assigns a value; a double == tests for equality. Conditions need ==.

Show solution

Why: Inside if (...) R expects a question (“is x equal to 3?”), which is ==. A single = tries to assign and is not allowed there, so R stops at the =.


3) Unbalanced parentheses or quotes

Every ( needs a matching ), and every " needs a closing ".

If your console prompt changes from > to +, R is not broken — it is waiting for the missing closing character. Press Esc to cancel, then fix the line.

Show solution
cat("Dose is", 75)

Why: The opening ( was never closed, so R keeps waiting for more input (the + prompt) instead of running the line. Add the closing ).


4) Missing commas between arguments

Arguments passed to a function must be separated by commas.

Show solution

Why: Without the comma, R reads "Batch" and "A" as two values jammed together with nothing joining them, and cannot tell where one argument ends and the next begins.


5) A stray space: x < -3 vs x <- 3

The assignment arrow <- is two characters with no space between them. Add a space and it becomes a comparison, < (less than) followed by -3.

Show solution

Why: x < -3 asks “is x less than -3?” — but x was never assigned, so R reports object 'x' not found. Removing the space restores the assignment arrow <-, which stores 3 in x.

Common R pitfalls

These run without a syntax error but give a surprising result — the trickier kind of bug, because R does not always warn you.

1) NA means “unknown”, so it spreads

NA marks a missing value. Any calculation involving NA returns NA, and you cannot test for it with ==.

Show solution

Why: mean() returns NA because one value is unknown — add na.rm = TRUE to ignore missing values. And x == NA cannot work (“is this equal to unknown?” is itself unknown), so use is.na(x) to find missing values.

2) Forgetting to assign the result

Most R functions return a new value and leave the original untouched. If you do not store the result, it is lost.

Show solution

Why: sort(x) computes a sorted copy and prints it, but does not change x. Assign the result back with x <- sort(x) to keep it.

3) library() vs install.packages()

These are easy to confuse, and mixing them up produces a could not find function error.

# Install ONCE per computer:
install.packages("tidyverse")

# Load EVERY new R session:
library(tidyverse)
Show solution

Why: install.packages() downloads a package to your computer — you only do it once. library() loads an installed package into your current session, and R forgets it when you restart, so you must run library(tidyverse) again at the top of every script. If R says it “could not find function”, the usual cause is a missing library() call.