CoursesR Basics

Reading Errors and Warnings

Lesson 8 of 8 · 12 min

Errors stop, warnings continue

R reports two kinds of trouble. An error means the instruction could not be carried out; nothing is assigned, and if the line was part of a script the script stops there. A warning means the instruction was carried out but something about it looked suspicious; you get a result, and it may or may not be the one you wanted. Errors are loud and easy to notice. Warnings are the dangerous ones, because the code keeps going with a value you did not inspect.

The four errors every beginner meets

Mean(c(1, 2, 3))
#> Error in Mean(c(1, 2, 3)) : could not find function "Mean"
sales
#> Error: object 'sales' not found
"5" + 2
#> Error in "5" + 2 : non-numeric argument to binary operator
mean(c(1, 2, 3)))
#> Error: unexpected ')' in "mean(c(1, 2, 3)))"
  • could not find function: the name is misspelled or has the wrong case (Mean is not mean), or the function lives in a package you have not loaded.
  • object not found: no variable with that name exists. Either you never assigned it, you typed it differently, or you meant a piece of text and forgot the quotation marks.
  • non-numeric argument to binary operator: an arithmetic operator received text. Somewhere a number is stored as a string, often because it came from a file with a stray character.
  • unexpected symbol, or unexpected ')': the line does not parse. Count brackets and quotes; a missing comma between arguments produces the same family of message.

Read the message from the front. Error in is followed by the call that failed, which tells you where; the part after the colon tells you what. Most messages are standard wording that has not changed in years, so pasting the exact text into a search engine usually finds an explanation within the first results.

Warnings and what they leave behind

as.numeric("abc")
#> Warning: NAs introduced by coercion
#> [1] NA
sqrt(-1)
#> Warning in sqrt(-1): NaNs produced
#> [1] NaN
c(1, 2, 3) + c(10, 20)
#> Warning in c(1, 2, 3) + c(10, 20): longer object length is not a multiple of shorter object length
#> [1] 11 22 13

Each warning comes with a result. as.numeric("abc") could not convert the text, so it produced NA, and the warning is the only hint that a value was lost; in a vector of a thousand entries you would not see it by eye. NaN, not a number, is what an impossible arithmetic result looks like. The recycling warning was covered in lesson three: R computed something, but not what you meant. In a longer session the warning text appears after the output, or is collected and shown as There were N warnings; warnings() prints them.

Problems that print nothing at all

Some results signal a problem with no message. Division by zero gives Inf, zero over zero gives NaN, and indexing past the end of a vector gives NA. All three flow silently into later calculations, where they turn a mean into NA ten lines later. When a summary comes out as NA or Inf, trace backwards: sum(is.na(x)) counts missing values, which(is.na(x)) locates them.

1 / 0
#> [1] Inf
0 / 0
#> [1] NaN
x <- c(1, 2, 3)
x[5]
#> [1] NA

A method for getting unstuck

  • Read the whole message, including the part after the colon, before changing anything.
  • Run the pieces of the failing line separately. If mean(temps[day == "Tue"]) fails, try day == "Tue" on its own, then the index, then the mean.
  • Check class() and length() of each input. Most errors are a value of the wrong type or a vector of the wrong length.
  • Check ls(), which lists the objects that exist. A name that is not in the list was never assigned in this session.
💡 Do not fix a warning by suppressing it. A warning names a specific problem; find the value that caused it, then decide whether the result is still valid.
Knowledge check
R prints Error: object 'sales' not found. What is the most likely cause?

Sign in to answer and track your progress.

Sign in