The “non-numeric argument to” error message is an easy problem to understand and one that is quite easy to fix. This message can result from either poorly written code or a problem with your data source. This does not mean that there is necessarily a mistake in the way the dataset is formatted but rather not handling it correctly. This does not necessarily mean you made a mistake in your coding but it could result from incorrect information or assumptions about the content of a dataset.
The Circumstances Of This Error
This problem occurs when an argument applied to a numeric function is not numeric. When working with a data frame it can occur if you apply a column to a numeric function that is not a numeric column.
> 1 + 2
[1] 3
In this simple example, we are applying numeric arguments to the equation and this produces the correct answer.
> 1 + “two”
Error in 1 + “two” : non-numeric argument to binary operator
In this simple example, we are applying a non numeric argument to the equation and this produces our message. These simple examples illustrate this problem perfectly. Furthermore, their simplicity makes understanding the nature of the problem extremely easy.
<h2>What is causing this error?</h2> This problem is caused by a conflict in data type. It is not an issue exclusive to a vector, matrix, data frame, or another type of dataset. It is entirely a question of whether or not an argument is a number or not.
> a = 150
> a+4
[1] 154
In this example, we are applying a numeric value to a simple addition equation. The result is that it provides the answer we are looking for. This is exactly the way this type of formula is intended to be used.
> a = “150”
> a+4
Error in a + 4 : non-numeric argument to binary operator
In this example, we are applying a non-numeric value to a simple addition equation. The result is that it provides our message. This is because this formula cannot take a non-numeric value. When it is given one it kicks out an error message. This is a simple problem to understand and it is an easy one to fix.
How To Fix This Error
There are three main ways of fixing this problem. They depend upon your access to and the nature of the data.<ul>
<li>If the dataset is part of your code simply correct the datatype so that you have a numeric value. </li>
<li>If you are using a data frame make sure but you are calling the right column. If you are calling the wrong column you may be calling another type rather than numbers.</li>
<li>Under other circumstances use a simple if statement to check the data type you are using and to correct it as needed. </li>
</ul> The following two segments of code illustrate the third option. It is the best one if you can not be sure of the consistency of your dataset.
> a = 150
> if (class(a) == “numeric”) a+4 else as.numeric(a)+4
[1] 154
In this example a is equal to a number. The if statement detects this and simply runs the addition routine.
> a = “150”
> if (class(a) == “numeric”) a+4 else as.numeric(a)+4
[1] 154
In this example a is equal to a string of number characters. The if statement detects this and simply runs the addition routine only after converting the string into a number. This will fix this problem for any case where the input is a number in the form of a string. If you try using a string that is not a number you will get an NA value and a warning message. However, as long as your input string is a number it will work.
This is an extremely simple problem. It results from the fact that mathematical functions will not accept a non-numeric argument. The two keys to fixing this problem are making sure that you are calling the right data. Calling the wrong column in a data frame can cause this problem. Once that has been eliminated then the solution is to check the data types and change them a needed