R Error: Non-numeric Argument to Binary Operator | How to Fix (Example)
This tutorial illustrates how to deal with the error message “non-numeric argument to binary operator” in the R programming language.
Please note that this page illustrates the error message returned by Base R. In case you are looking for the error message “Error in max.item – min.item : non-numeric argument to binary operator” returned by the alpha function of the psych package, you may have a look here.
Table of contents:
Let’s dive right into the example!
Example: Reproducing & Fixing the Error Message: non-numeric argument to binary operator
In this Example, I’ll explain how to reproduce and fix the error message “non-numeric argument to binary operator”. First, let’s create an example that reproduces the error:
5 * "three" # Trying to use character string in equation # Error in 5 * "three" : non-numeric argument to binary operator |
5 * "three" # Trying to use character string in equation # Error in 5 * "three" : non-numeric argument to binary operator
As you can see based on the previous R code, we have tried to use a character string in an equation (i.e. “three”). Unfortunately, this is not possible in the R programming language.
If we want to solve this problem, we need to replace the character string by a numeric value (i.e. 3). Let’s do that:
5 * 3 # Proper equation # 15 |
5 * 3 # Proper equation # 15
As you can see, the RStudio console is not returning the error anymore. Instead it shows the result of our equation.
Video & Further Resources
Would you like to learn more about error messages in R? Then you might have a look at the following video of my YouTube channel. I explain the R programming code of this page in the video instruction:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Besides the video, you could have a look at the other articles of my website.
Summary: In this article you learned how to handle the error non-numeric argument to binary operator in the R programming language. Tell me about it in the comments, in case you have additional questions.
10 Comments. Leave new
In need help for a test cromcah’s alpha: Error:non-numeric argument to binary operator, how I can resolve it?
Hey Hubermane,
Could you illustrate how your data looks like and share the code you have used?
Thanks
Joachim
Hi Joachim, I’m in the same boat as Hubermane. I’ve used this syntax with other measures and haven’t run into the same issue. My code:
d_resist$res_1 <- varRecode(d_resist$mses_30_1, c("Not at all", "A little", "Somewhat", "Quite a bit", "Very much"), c(1, 2, 3, 4, 5))
# same for items 2-21
res_items <- c("res_1", "res_2", "res_3", "res_4", "res_5", "res_6", "res_7", "res_8", "res_9","res_10", "res_11", "res_12", "res_13", "res_14", "res_15", "res_16", "res_17", "res_18", "res_19", "res_20", "res_21")
psych::alpha(d_resist[, res_items])
"Error in max.item – min.item : non-numeric argument to binary operator"
I'm assuming it's because the recoded items are factors with 5 levels and not numeric, and the vector is a character vector, but the same was true of other measures and I didn't get an error message.
I'd be grateful for your insight!
Hi Tidy,
Thank you for the detailed question.
I think you are on the right track – it seems like this error is coming from the factor class in your data.
I have created a new tutorial, which explains how to handle this error (the error shown on this package is actually different).
You can find the tutorial here: https://statisticsglobe.com/r-error-in-max-min-item-non-numeric-argument
I hope that helps!
Joachim
Hie
I am getting this error
‘Error in colm5 non numeric argument to binary operator.
This is my function
pol.plot <- strat.plot(pollen.ok, y.tks.label=y.scale, y.rev=TRUE,srt.xlabel=45, plot.line=FALSE, plot.poly=TRUE, plot.bar=TRUE, col.bar="black", col.poly=p.col, col.poly.line="black", scale.percent=TRUE, xSpace=0.01, x.pc.lab=TRUE, x.pc.omit0=TRUE, las=2)
Hey Charmaine,
It seems like the columns in your data don’t have the correct class. You could check the classes of all columns using the following code:
Regards,
Joachim
Error in start[2L] – 1 : non-numeric argument to binary operator
Hii, i am getting this error and i am unable to figure out where I went wrong. mY syntax was as follows:
variable_fdi <- ts(FDI, start= c("2016", 4),end = c("2021", 3), frequency = 12)
Hey Kritika,
Could you illustrate the data contained in the object FDI?
Regards
Joachim
hi Joachim
I’m trying to use any three algorithms to detect anomalies in my data so far i have used Seasonal hybrid ESD algorithm and its only showing 2 anomalies instead of all the thermal anomalies .can you help me with suggesting any other algorithms i can use in both R and Python
Hey Nigel,
I’m not an expert on this topic. However, I have recently created a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim