# How to Fix non-numeric argument to binary operator in R

A binary operator in R is a symbol used to execute a specific mathematical or logical operation on two variables or expressions.

## What is a Numeric Vector in R?

A numeric vector is a one-dimensional array of numbers that can be created using either c() function or the colon(:) operator.

``````rnum <- c(1, 2, 3, 4, 5)
rnum_second <- 1:5

print(rnum)
print(rnum_second)``````

### Output

`````` 1 2 3 4 5
 1 2 3 4 5``````

## What is a Character Vector in R?

A character vector in R is a one-dimensional array of character strings that can be created using the c() function.

``````rchar <- c("chatgpt", "gpt-3", "gpt-4")

print(rchar)``````

### Output

`` "chatgpt" "gpt-3" "gpt-4"``

If you use a binary operator on a character vector, you will face an error because the binary operator performs well on the numeric vector and does not work on the character vector.

Let’s discuss this error and how to fix it in R.

## non-numeric argument to binary operator in R

The non-numeric argument to binary operator error occurs in R when a binary operator (such as +, -, *, etc.) is used with non-numeric arguments.

The error is raised if one or both of the arguments passed to the operator are not of a numeric data type (e.g., character or logical).

Check out the following code that raises Error: non-numeric argument to binary operator.

``````a <- "21"
b <- 19
c <- a + b

print(c)``````

#### Output

``Error in a + b : non-numeric argument to binary operator``

In the above code, the variable “a” is a character, and “b” is numeric, so when we try to add them together, R will generate an error message because we are trying to add a character and a numeric.

## How to fix Error: non-numeric argument to binary operator

To fix Error: non-numeric argument to binary operator in R, use the as.numeric() function to the character to a numeric vector.

``````a <- "21"
b <- 19
c <- as.numeric(a) + b

print(c)``````

#### Output

`` 40``

You can see that it solved the error and returned the expected output.

## Conclusion

The Error: non-numeric argument to the binary operator in R is raised when executing a binary operation on character or non-numeric vectors. The easy fix is to convert the non-numeric vector to a numeric vector using the as.numeric() function.