R is a popular programming language used in data analysis and statistics. It provides us with lots of built-in functions and libraries that help to perform various complex computations on large datasets. However, sometimes you might encounter an error “non-numeric argument to binary operator”. And if you are a beginner, then this error can be very frustrating and confusing to resolve.
Table of Contents
Here, in this article, we will discuss the cause of the error and the solution to solve the error.
Before proceeding further let’s understand about the binary operator in R a little.
What are Binary Operators in R?
In R, a binary operator is an operator that operates on two operands and manipulates the operands and returns us a result.
The fours operators are:
- Addition ( + )
- Subtraction ( – )
- Multiplication ( * )
- Division ( / )
These binary operators need to have the same type of operands i.e either numeric or character to perform binary operations.
What is the reason for “Non-Numeric Argument to Binary Operator” Error?
The “Non-numeric argument to binary operator” error occurs when we do binary operations on two operands that are of different types. That means when we try to perform mathematical operations on a non-numeric variable. For example, you are trying to add a numeric value with a character value.
num1 <- "2"
num2 <- 2
res <- num1 + num2
print(res)
The will give us the error
Error in num1 + num2 : non-numeric argument to binary operator
Execution halted
Another reason could be, the data type of the variable is not what the operator was expecting. For example, when you are trying to import a dataset that contains some non-numeric values, then the operator won’t be able to perform the calculation and throw us the error.
How to fix “Non-Numeric Argument to Binary Operator” error in R?
There are three different solutions we can try to fix the error. They are:
- Check the datatype of the variable using
class()
function. - Convert non-numeric value to numeric value using
as.numeric()
function. - Using
ifelse()
function
Solution 1: Check the Data Type using class() function
In R programming language, we can use the class()
function to check the data type of the variables.
For example, let’s check the data type of the two variable below:
num1 <- 2
num2 <- "2"
print(class(num1))
print(class(num2))
The output will be
[1] "numeric"
[1] "character"
Now let’s write a program that checks the data type of both variables and performs binary operations only if both variables are numeric. We will use the if..else
statement to write a condition to return the result.
num1 <- 3
num2 <- "2"
if (class(num1) == "numeric" && class(num2) == "numeric") {
res <- num1 + num2
print(res)
} else {
print("Both num1 and num2 must be numeric")
}
The output wil be:
"Both num1 and num2 must be numeric"
Since num2
variable was a character, the condition was False
, and so we go the “Both num1 and num2 must be numeric” as the output.
Solution 2: Convert non-numeric value to numeric using as.numeric()
function.
Now in R language, if we have to perform a mathematical operation on a non-numeric value then we have to convert it to a numeric value using the as.numeric()
function.
For example:
num1 <- 3
num2 <- "2"
res <- num1 + as.numeric(num2)
print(res) # 5
Here, in the above example, we have converted the character value, num2
into a numeric value using as.numeric()
function and then added both variables. The output is 5.
Solution 3: Using ifelse() function
In R, we can use the conditional operation on a variable using ifelse()
function.
The ifelse()
function takes in three arguments:
- The condition
- The value that will return if the condition is
True
, and - The value that will return if the condition is
False
Syntax:
ifelse(condition, True value, False value)
Let’s write a function to check if the given variable is a numeric value or not using is.numeric()
function. If the value is numeric (condition is True), we will perform an addition operation on it, if the variable is non-numeric (False), then we will show an error.
The
is.numeric()
function returnTrue
if the value is of numeric data type andFalse
otherwise.
num <- 4
x <- ifelse(is.numeric(num), num+1, "The value is non-numeric")
print(x) # Output: 5
Here the variable num
is a numeric variable, so the condition is True
and we get 5 as the output.
If the variable was of character data type the condition would have been False
and we would get “The value is non-numeric” error message as the output.
Conclusion:
As a beginner, the “non-numeric argument to binary operator” error can be very frustrating. However, if you know the main reason for the error and understand how the binary operator works it can be easily fixed.
By checking the data type, converting the non-numeric value to a numeric value, and by using the ifelse() function we can resolve the error in R programming language.
Hope, the above solutions have helped you fix the error in your code.