How to Know When to Care About Warnings in R

Errors and warnings have different functions in R You can’t get around errors, because they just stop your code. Warnings on the other hand are a whole different beast. Even if R throws a warning, it continues to execute the code regardless. You can ignore warnings, but generally that’s a pretty bad idea. Warnings often are the only sign you have that your code has some semantic or logic error.

For example, you could’ve forgotten about the ifelse() function and tried something like the following example:

> x <- 1:10
> y <- if (x < 5 ) 0 else 1
Warning message:
In if (x < 5) 0 else 1 :
the condition has length > 1 and only the first element will be used

This warning points at a semantic error: if expects a single TRUE or FALSE value, but you provided a whole vector. Note that, just like errors, warnings tell you in general which code has generated the warning.

Here is another warning that pops up regularly and may point to a semantic or logic error in your code:

Because x - 5 is negative when x is 4, R cannot calculate the square root and warns you that the square root of a negative number is not a number (NaN).

If you’re a mathematician, you may point out that the square root of –1 is 0 - 1i. R can, in fact, do calculations on complex numbers, but then you have to define your variables as complex numbers. You can check, for example, the Help file ?complex for more information.

Although most warnings result from either semantic or logic errors in your code, even a simple syntax error can generate a warning instead of an error. If you want to plot some points in R, you use the plot() function. It takes an argument col to specify the color of the points, but you could mistakenly try to color the points using the following:

> plot(1:10, 10:1, color='green')

If you try this, you get six warning messages at once, all telling you that color is probably not the argument name you were looking for:

Warning messages:
1: In plot.window(...) : "color" is not a graphical parameter
2: In plot.xy(xy, type, ...) : "color" is not a graphical parameter
....

Notice that the warning messages don’t point toward the code you typed at the command line; instead, they point to functions you never used before, like plot.window() and plot.xy().

You can pass arguments from one function to another using the dots argument. That’s exactly what plot() does here. So, plot() itself doesn’t generate a warning, but every function that plot() passes the color argument to does.

If you get warning or error messages, a thorough look at the Help pages of the function(s) that generated the error can help in determining what the reason is for the message you got. For example, at the Help page of ?plot.xy, you find that the correct name for the argument is col.

So, to summarize, most warnings point to one of the following problems:

The function gave you a result, but for some reason that result may not be correct.

The function generated an atypical outcome, like NA or NaN values.

The function couldn’t deal with some of the arguments and ignored them.

Only the last one tells you there’s a problem with your syntax. For the other ones, you have to examine your code a bit more.