When writing a computer program, it is common to run across cases where the content of variables is not what you expect. This is particularly the case when you do not have control over the data source. One reason this can occur is that numbers can be entered as characters. This is the problem at the heart of this error message.
Description Of The Error
The non-numeric argument to binary operator error message occurs when you try to enter a character into an operation that is looking for numerical data. It is called a binary operator error because numbers are stored in a binary fashion rather than as a string of characters. An operator is a programming command that works on data. An argument is the data being worked on. They can work on a data frame as well as individual variables and constants. The key to understanding this error message is that you are entering the wrong type into the command, because of that fixing it requires correcting that problem.
Explanation Of The Error
The reason why the non-numeric argument to binary operator error message occurs is that you are trying to enter a non numeric argument into a mathematical function. The binary operator error occurs because the function is looking for a numeric value. A binary operator is any type of mathematical operation. A value entered into a mathematical operation has to be a number. The variable containing the value can be a data frame or the output of the cbind() function. All that matters is that the values being used are numbers. If they are not numbers, you will get a binary operator error message.
Example Of The Error
Here is an example of code that produces this error message.
> a = 1
> b = “2”
> c = a + b
Error in a + b : non-numeric argument to binary operator
In this example, you will note that a contains a numeric value of one. However, <i>b</i> contains a character value of “2”. Now the code is trying to add a number to a character, and it cannot do that. As a result, we get our error message.
How To Fix The Error
While the simplest way to fix this problem is to simply delete the character string, this is not the best way to deal with an r error.
> a = 1
> b = “2”
> c = a + as.numeric(b)
> c
[1] 3
In this example, we use the as.numeric() function to convert the number character to a numeric value. As a result of this simple operation, the error has been fixed. In this case, we are working with byte values, but you could also be working with a row of values. This situation is most likely to occur if you are loading in data from an external file or values that are entered by a user. In either case, this little trick will ensure that you are only working with numeric values.
Error messages are an unfortunate part of programming, and unfortunately fixing the problem does not always mean fixing the data source. As a result, being able to convert between numeric and character data types is an important part of making sure that you have the right data type. By becoming familiar with these types of functions, you will avoid this type of error message and have no need of fixing it.