What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?

What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?

The error “non-numeric argument to binary operator” in R refers to an issue with the function Fix, which is used to modify and correct data in a dataset. This error occurs when the Fix function is applied to a non-numeric data type, such as a character or logical value, instead of a numeric value. This means that the Fix function can only be used on numeric data and will not work on other types of data. To resolve this error, the non-numeric data must be converted to a numeric type before applying the Fix function.

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 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

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

How to Fix in R: longer object length is not a multiple of shorter object length

Cite this article

stats writer (2024). What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-does-the-error-non-numeric-argument-to-binary-operator-mean-when-using-the-function-fix-in-r/

stats writer. "What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/what-does-the-error-non-numeric-argument-to-binary-operator-mean-when-using-the-function-fix-in-r/.

stats writer. "What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-does-the-error-non-numeric-argument-to-binary-operator-mean-when-using-the-function-fix-in-r/.

stats writer (2024) 'What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-does-the-error-non-numeric-argument-to-binary-operator-mean-when-using-the-function-fix-in-r/.

[1] stats writer, "What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. What does the error “non-numeric argument to binary operator” mean when using the function Fix in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top