# How To Fix the R error non-numeric argument to

The “non-numeric argument to” error message in the R programming language is an easy problem to understand and one that is quite easy to fix. This message can result from either poorly written R 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 variable content of a dataset.

## The circumstances of this error.

This problem occurs when an argument applied to a numeric function is not a logical value or numeric vector. When working with a data frame it can occur if you apply a column to a numeric vector function that is not a numeric column.

``````# r error non-numeric argument to
> 1 + 2
 3``````

In this simple example, we are applying numeric arguments to the equation and this produces the correct answer.

``````# non-numeric argument to r error
> 1 + "two"
Error in 1 + "two" : non-numeric argument to binary operator``````

In this simple character string 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.

## What is causing this error?

This problem output is caused by a conflict in data type in your R code. 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 or object is a number variable or not.

``````# non-numeric argument to error in r
> a = 150
> a+4
 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.

``````# r programming error non-numeric argument to
> a = "150"
> a+4
Error in a + 4 : non-numeric argument to binary operator``````

In this example, we are applying a non-numeric value object to a simple addition equation. The output result is that it provides our message. This is because this mathematical function 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.

• If the dataset is part of your code simply correct the datatype so that you have a logical value or integer.
• If you are using a data frame make sure but you are calling the right column names. If you are calling the wrong column names you may be calling another type rather than numbers.
• Under other circumstances use a simple if statement to check the data type you are using and to correct the data file as needed.

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.

``````# how to fix non-numeric argument to error in r
> a = 150
> if (class(a) == "numeric") a+4 else as.numeric(a)+4
 154``````

In this example a is equal to a number. The if statement detects this and simply runs the addition routine.

``````# non-numeric argument to r error solution
> a = "150"
> if (class(a) == "numeric") a+4 else as.numeric(a)+4
 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 to fix in the R programming language. It results from the fact that your mathematical function will not accept a non-numeric argument or character string. The two keys to fixing this problem are making sure that you are calling the right data. Calling the wrong column in a dataframe can cause this problem. Once that has been eliminated then the solution is to check the data types and change them as needed.

Looking for more help with R programming errors? Check out these other great articles:

Scroll to top