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

# 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 (â€“)
• 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 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 ```

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

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