How to Read Errors and Warnings in R
If something goes wrong with your code, R tells you. We have to admit it: These error messages can range from mildly confusing to completely incomprehensible if you’re not used to them. But it doesn’t have to stay that way. When you get familiar with the error and warning messages from R, you can quickly tell what’s wrong.
Errors can happen in two ways:
The code keeps on running until the end, and when the code is finished, R prints out a warning message.
The code stops immediately because R can’t carry it out, and R prints out an error message.
Read error messages
Let’s take a look at such an error message. If you try the following code, you get this more or less clear error message:
> "a" + 1 Error in "a" + 1 : non-numeric argument to binary operator
You get two bits of information in this error message. First, the line “a” + 1 tells you in which line of code you have an error. Then it tells you what the error is. In this case, you used a non-numeric argument (the character ‘a’): In combination with a binary operator (the + sign).
R always tells you in which code the error occurs, so you know in many cases where you have to start looking.
Error messages aren’t always that clear. Take a look at the following example:
> data.frame(1:10,10:1,) Error in data.frame(1:10, 10:1, ) : argument is missing, with no default
To what argument does this error refer? Actually, it refers to an empty argument you provided for the function. After the second vector, there’s a comma that shouldn’t be there. A small typing error, but R expects another argument after that comma and doesn’t find one.
If you don’t immediately understand an error message, take a closer look at the things the error message is talking about. Chances are, you just typed something wrong there.