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


One error you may encounter in R is:

Error in df$var1- df$var2: non-numeric argument to binary operator 

This error occurs when you attempt to perform some binary operation on two vectors and one of the vectors is non-numeric.

Examples of binary operations include:

  • Subtraction ()
  • Addition (+)
  • Multiplication (*)
  • Division (/)

This error occurs most often when one of the vectors you provide is a character vector.

This tutorial shares exactly how to fix this error.

How to Reproduce the Error

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(period = c(1, 2, 3, 4, 5, 6, 7, 8),
                 sales = c(14, 13, 10, 11, 19, 9, 8, 7),
                 returns = c('1', '0', '2', '1', '1', '2', '2', '3'))

#view data frame
df

  period sales returns
1      1    14       1
2      2    13       0
3      3    10       2
4      4    11       1
5      5    19       1
6      6     9       2
7      7     8       2
8      8     7       3

Now suppose we attempt to create a new column called ‘net’ by subtracting the ‘returns’ column from the ‘sales’ column:

#attempt to create new column called 'net'
df$net <- df$sales - df$returns

Error in df$sales * df$returns : non-numeric argument to binary operator

An error occurs because the ‘returns’ column is of the class ‘character’ and it’s not possible to subtract a character column from a numeric column.

#display class of 'sales' column
class(df$sales)

[1] "numeric"

#display class of 'returns' column
class(df$returns)

[1] "character"

How to Fix the Error

The way to fix this error is to use as.numeric() to convert the ‘returns’ column to numeric before performing the subtraction:

#create new column called 'net'
df$net <- df$sales - as.numeric(df$returns)

#view updated data frame
df

  period sales returns net
1      1    14       1  13
2      2    13       0  13
3      3    10       2   8
4      4    11       1  10
5      5    19       1  18
6      6     9       2   7
7      7     8       2   6
8      8     7       3   4

We’re able to perform the subtraction without any errors because both the ‘sales’ and the ‘returns’ columns were numeric.

Additional Resources

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: dim(X) must have a positive length
How to Fix in R: names do not match previous names
How to Fix in R: longer object length is not a multiple of shorter object length
How to Fix in R: contrasts can be applied only to factors with 2 or more levels

Leave a Reply

Your email address will not be published.