6.9 C
London
Thursday, December 19, 2024
HomeRFix Common Errors in RHow to Fix in R: non-numeric argument to binary operator

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories