How to Fix the R error in -0.01 * height : non-numeric argument to binary operator

All R functions will produce errors under the right circumstances. However, the “non-numeric argument to binary operator” error message results from a problem with a binary operation rather than a function. These are basic operations such as addition, subtraction, multiplication, and division. It occurs if one of your inputs is not a numeric value.

Description of the R Error

Here we have three examples of this operation working properly. In all three cases, both of the inputs are numeric.

> height = c(3, 5, 6, 8, 9)
> class(height)
[1] “numeric”
> z = -0.01 * height
> z
[1] -0.03 -0.05 -0.06 -0.08 -0.09

Here is an example where we have a single vector multiplied by a constant. No error occurs in this case because the input is a numeric vector.

> weight = c(145, 130, 280, 200, 170)
> height = c(64, 59, 73, 70, 68)
> class(weight)
[1] “numeric”
> class(height)
[1] “numeric”
> z = weight / height
> z
[1] 2.265625 2.203390 3.835616 2.857143 2.500000

This example illustrates an input consisting of two numeric vectors. Likewise, it does not produce an error because both vectors are numeric.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(5)
> y = rnorm(5)
> class(x)
[1] “numeric”
> class(y)
[1] “numeric”
> z = x * y
> z
[1] 1.219151601 0.179637530 -0.414126535 1.033025935 -0.001528776

In this example, our input is two randomly generated numeric vectors. Once again, they do not produce an error because they are both numeric vectors. The point of this example is to demonstrate the fact that the data itself does not matter, but rather the data type.

Explanation of the R error

Here we have the same three examples but we have set one of the vectors to cause our error message by making it a character vector. It needs to be noted that you get a different message when working with a data frame instead of vectors. If the data frame contains a column of characters it will trigger the “Warning message: In Ops.factor(df$weight, df$height) : ‘/’ not meaningful for factors” instead. It is of value that the fix for this problem is the same as for our error message.

> height = c(“3”, “5”, “6”, “8”, “9”)
> class(height)
[1] “character”
> z = -0.01 * height
Error in -0.01 * height : non-numeric argument to binary operator

In this example, we reproduce the error message exactly. Note that we are using a character vector, rather than a numeric and this is triggering the error.

> weight = c(145, 130, 280, 200, 170)
> height = c(“64”, “59”, “73”, “70”, “68”)
> class(weight)
[1] “numeric”
> class(height)
[1] “character”
> z = weight / height
Error in weight/height : non-numeric argument to binary operator

In this example, we have two vectors, one of which is numeric and the other is a character vector. The data is the same as before but it is set up as a character vector causing our error message.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(5)
> y = as.character(rnorm(5))
> class(x)
[1] “numeric”
> class(y)
[1] “character”
> z = x * y
Error in x * y : non-numeric argument to binary operator

In this example, we have two random vectors, one of which is converted to a character vector. This demonstrates the importance of the format and not the data content.

How to fix the R error

Here we show how to fix the problem in all three cases. To fix this problem we use the as numeric function to convert the character vector to a numeric value vector.

> height = c(“3”, “5”, “6”, “8”, “9”)
> class(height)
[1] “character”
> class(as.numeric(height))
[1] “numeric”
> z = -0.01 * as.numeric(height)
> z
[1] -0.03 -0.05 -0.06 -0.08 -0.09

> weight = c(145, 130, 280, 200, 170)
> height = c(“64”, “59”, “73”, “70”, “68”)
> class(weight)
[1] “numeric”
> class(as.numeric(height))
[1] “numeric”
> z = weight / as.numeric(height)
> z
[1] 2.265625 2.203390 3.835616 2.857143 2.500000

> t = as.numeric(Sys.time())
> set.seed(t)
> x = rnorm(5)
> y = as.character(rnorm(5))
> class(x)
[1] “numeric”
> class(y)
[1] “character”
> class(as.numeric(y))
[1] “numeric”
> z = x * as.numeric(y)
> z
[1] 1.219151601 0.179637530 -0.414126535 1.033025935 -0.001528776

This is an extremely easy error to fix. it is a simple matter of converting a character vector into a numeric vector. It will come in handy if you do not have control over the data you are using. If you do have control over it, it is best to avoid the problem by making sure that the data is numeric.

Scroll to top