Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Objectives

By the end of this lesson you will be able to:

  1. Read an R error message for what it literally says, before assuming the worst (the habit started in Lesson 4).

  2. Recognize the handful of errors that account for most beginner mistakes — misspelled object/function names, a forgotten library() call, a mismatched file path, and a few others.

  3. Use a troubleshooting table to go from “R turned red” to a fix, fast.

Every error and every “gotcha” on this page is real — each one was actually triggered by running the broken code shown, in R 4.5.2, with library(mosaic) and library(BSDA) loaded (or deliberately not loaded, for one of them). Nothing here is paraphrased or guessed at; it’s exactly what you’ll see on your own screen if you make the same slip.

2The troubleshooting table

Start here. Match what you’re seeing to the left column, then jump to that section below for the full before/after.

What R says (or does)What’s really wrongFix
Error: object 'x' not foundTypo in an object name, or you never ran the line that created itCheck the spelling against where you created it; re-run that earlier line
could not find function "f"You forgot library(mosaic) (or library(BSDA)) this sessionRun both library() lines, then re-run your code
non-numeric argument to binary operatorYou tried math (+, -, ...) on a text/category columnOnly do arithmetic on numeric columns; use categories for grouping, not math
object 'x' not found (inside a formula)data = points at the wrong data frame — that column lives somewhere elseDouble-check which data frame actually has that column (names(df))
Confusing column name in the output, like coffee$day_typeMixed $ and the ~ formula togetherUse clean y ~ x with data = — never df$x inside a formula that also has data =
Numbers come out completely wrong, no error at allas.numeric() on a factor gives level positions, not the text’s valueGo through as.character() first: as.numeric(as.character(x))
cannot open file 'x': No such file or directoryThe path doesn’t match where the file actually isCheck your working folder; use the full relative path (e.g. "data/x.csv")

The rest of this lesson walks through each row with the real broken code, the real message, and the real fix.

31. Object not found (a typo)

This is the single most common error in R, and you already met it in Lesson 4:

wait_times <- c(3, 5, 2, 8, 4)
mean(wiat_times)
Error: object 'wiat_times' not found

Read it literally: R is telling you, plainly, that nothing named wiat_times exists. Compare the name in the error to the name you actually created (wait_times) and the typo jumps out. Fix: spell it correctly.

mean(wait_times)
[1] 4.4

42. could not find function (you forgot library())

You met this one in Lesson 5. It happens whenever you call a mosaic or BSDA function before turning those packages on for the current session — for example, right after restarting R, before re-running your library() lines:

coffee <- read.csv("data/coffee_wait_sim.csv")
favstats(~ wait_minutes, data = coffee)
Error in favstats(~wait_minutes, data = coffee) : 
  could not find function "favstats"

read.csv() worked fine (it’s base R — always available); favstats() failed because it lives inside mosaic, which was never loaded in this session. Fix: load it first.

library(mosaic)
library(BSDA)
favstats(~ wait_minutes, data = coffee)
 min    Q1 median    Q3 max mean       sd  n missing
 1.4 2.525    3.7 5.825 8.1    4 1.855291 20       0

53. Non-numeric argument to a binary operator

“Binary operator” just means a symbol like + that combines two things. This error fires when one of those two things is text or a category instead of a number — for example, accidentally adding a numeric column and a character column instead of grouping by one and summarizing the other:

coffee$wait_minutes + coffee$day_type
Error in coffee$wait_minutes + coffee$day_type : 
  non-numeric argument to binary operator

day_type holds text ("Weekday", "Weekend") — R has no idea how to add a number to the word "Weekend", and says so directly. This usually means you reached for + when you actually wanted to compare groups, not combine them. Fix: use the categorical column the way Lesson 6 taught — as the right-hand side of a formula, grouping the numeric column instead of adding it:

favstats(wait_minutes ~ day_type, data = coffee)
  day_type min    Q1 median  Q3 max     mean       sd  n missing
1  Weekday 2.3 3.675   4.45 5.9 8.1 4.716667 1.752833 12       0
2  Weekend 1.4 2.100   2.70 3.1 6.2 2.925000 1.521043  8       0

64. Wrong data = (the column lives somewhere else)

This happens when you have more than one dataset loaded and accidentally point data = at the wrong one — the variable you want exists, just not in that data frame:

data(KidsFeet)
favstats(~ wait_minutes, data = KidsFeet)
Error in eval(formula[[2]], data, .envir) : 
  object 'wait_minutes' not found
Calls: favstats -> maggregate -> FUN -> eval -> eval

This one looks scarier than error #1 above because of the Calls: line — that’s R showing you the chain of internal functions favstats() used along the way. You can ignore that line completely; the part that matters is still just object 'wait_minutes' not found (KidsFeet has length and width, not wait_minutes — that column lives in coffee). Fix: point data = at the data frame that actually has the column, which you can always double-check with names():

names(KidsFeet)
[1] "name"       "birthmonth" "birthyear"  "length"     "width"     
[6] "sex"        "biggerfoot" "domhand"   
favstats(~ wait_minutes, data = coffee)
 min    Q1 median    Q3 max mean       sd  n missing
 1.4 2.525    3.7 5.825 8.1    4 1.855291 20       0

75. $ vs. the ~ formula

Unlike the errors above, this one usually does not turn red — it quietly runs and gives you a confusing result, which can be worse, since nothing flags it as a mistake. It happens when you mix df$column inside a formula that also has data =:

favstats(coffee$wait_minutes ~ coffee$day_type, data = coffee)
  coffee$day_type min    Q1 median  Q3 max     mean       sd  n missing
1         Weekday 2.3 3.675   4.45 5.9 8.1 4.716667 1.752833 12       0
2         Weekend 1.4 2.100   2.70 3.1 6.2 2.925000 1.521043  8       0

Look closely at the grouping column’s name in the output: coffee$day_type, not the clean day_type you’d expect. The numbers happen to be correct here — but the ugly, repeated coffee$ in the header is a sign you’re fighting the formula grammar instead of using it, and in other functions this same habit does cause real errors (or plots with broken axis labels). Fix: data = already tells R which data frame to use — inside the formula, use bare column names only, no $:

favstats(wait_minutes ~ day_type, data = coffee)
  day_type min    Q1 median  Q3 max     mean       sd  n missing
1  Weekday 2.3 3.675   4.45 5.9 8.1 4.716667 1.752833 12       0
2  Weekend 1.4 2.100   2.70 3.1 6.2 2.925000 1.521043  8       0

Same numbers, clean labels — this is the version to actually use.

86. Factor vs. numeric (a silent trap, no error at all)

This is the sneakiest one on this page, because R never complains — it just hands back the wrong numbers. A factor is R’s way of storing categories; if a column of numbers gets read in as a factor (or you build one from text on purpose), converting it with as.numeric() does not give you the numbers you typed — it gives you each value’s position in the factor’s alphabetical list of levels:

codes <- factor(c("5", "10", "20"))
codes
[1] 5  10 20
Levels: 10 20 5
as.numeric(codes)
[1] 3 1 2

Read the Levels: line: R sorted "10", "20", "5" alphabetically (as text, “1” comes before “2” comes before “5”), so "5" became level 3, "10" became level 1, and "20" became level 2 — hence 3 1 2, not the 5 10 20 you’d expect. This is exactly why Lesson 6 has you run str() on every new dataset: it shows you when a column that looks numeric actually got stored as a factor or as text (chr). Fix: convert to text first, then to numeric, so R uses the actual characters instead of the level position:

as.numeric(as.character(codes))
[1]  5 10 20

97. File not found

The last one is exactly the situation flagged back in Lesson 6: read.csv()'s path has to match where the file actually is, relative to your script or notebook’s own folder.

read.csv("coffee_wait_sim.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'coffee_wait_sim.csv': No such file or directory

The real file lives inside a data/ folder, not next to the script directly — this line left that folder name off. Fix: include the full relative path.

read.csv("data/coffee_wait_sim.csv")
  customer_id wait_minutes day_type
1           1          3.7  Weekday
2           2          2.3  Weekday
3           3          1.4  Weekend
4           4          3.7  Weekend
5           5          5.9  Weekday
6           6          6.2  Weekend

10Summary

SymptomCauseFix
object 'x' not foundTypo, or the line that made x never ranFix the spelling; re-run the earlier line
could not find function "f"Forgot library(mosaic) / library(BSDA)Run both library() lines first
non-numeric argument to binary operatorArithmetic on a text/category columnUse it to group (~), not to do math
object 'x' not found inside a formuladata = points at the wrong data frameCheck with names(df); use the right one
Ugly df$column in your output’s labelsMixed $ with a ~ formula that also has data =Use bare column names inside the formula
Numbers silently wrong, no erroras.numeric() on a factoras.numeric(as.character(x))
cannot open file, No such file or directoryPath doesn’t match the file’s real locationInclude the folder, e.g. "data/x.csv"

Every one of these is ordinary — even professional R programmers hit all seven regularly. The skill this lesson teaches isn’t “never make these mistakes”; it’s reading the message for what it actually says, checking it against this table, and fixing the one specific thing it points at.

11Check your understanding

  1. You run favstats(~ score, data = quiz) and get Error: object 'score' not found. Give two different, unrelated reasons this exact message could appear, and how you’d tell which one is really going on.

  2. What’s the difference between the error in section 3 (non-numeric argument) and the silent problem in section 6 (factor vs. numeric) — why is the second one arguably more dangerous?

  3. A classmate writes favstats(exam$score ~ exam$section, data = exam) and it runs without a red error. What’s still wrong with it, and how do you know just from looking at the output?

  4. You get cannot open file 'grades.csv': No such file or directory. Name two different possible fixes, depending on what’s actually wrong.