Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Simple solutions for the error in fun(left, right) : non-numeric argument to binary operator error
Next
Answered
Lou Munoz
  • 34
Lou Munoz
Asked: May 18, 20222022-05-18T10:51:00+00:00 2022-05-18T10:51:00+00:00In: r

Simple solutions for the error in fun(left, right) : non-numeric argument to binary operator error

  • 34

. Advertisement .

..3..

. Advertisement .

..4..

Here is the program I run:

Transaction <- function(PnL, Day)
  results <- list(a = PnL, b = Day)
  return(results)

 Day <- Transaction(PnL, Day)["b"]

 moving_avg <- function(StockData, MA, CurrentDay){
  #MA = Days long the MA is
  #CurrentDay = What day we are currently on
  MAValue <- NULL
  total <- 0
  start <- CurrentDay - MA
  for(i in 1:length(MA)) {
  total <- total + StockData[[start, 4]]
  start <- start + 1
  }
  MAValue <- total/MA
  return(MAValue)
 }

After I run, it returns an error:

Error in CurrentDay - MA : non-numeric argument to binary operator

Does anyone have any suggestions for the problem below: error in fun(left, right) : non-numeric argument to binary operator in the r – How to correct it?

non-numeric argument to binary operator
  • 2 2 Answers
  • 104 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-08T09:30:43+00:00Added an answer on June 8, 2022 at 9:30 am

    The cause:

    This error occurs because you are trying to perform - as the ‘binary operator’, but CurrentDay or MA is non-numeric, even both of them are non-numeric.

    Solution:

    This error will be solved if you run the following program before your code:

    '+' <- function(e1, e2) {
    if (is.character(e1) | is.character(e2)) {
    paste0(e1, e2)
    } else {
    base::`+`(e1, e2)
    }
    }
    • 7
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Gabin Tournier
    2022-05-25T20:18:29+00:00Added an answer on May 25, 2022 at 8:18 pm

    Your question is not about your function, but rather the error message. I will address it.

    - refers to the ‘binary operator’ that your error is referencing. CurrentDay and MA are both non-numeric.

    Binary operations are calculations that take two values (operands), and produce another value ( refer to Wikipedia for more). + is an example of such an operator. “1 + 1” takes 2 operands (1 & 1) and returns another (2). The produced value doesn’t necessarily differ from the operands, e.g. 1 + 0 = 1.

    R knows only how to apply + and other binary operators such as - to numeric arguments.

    > 1 + 1
    [1] 2
    > 1 + 'one'
    Error in 1 + "one" : non-numeric argument to binary operator

    If you see this error message, it is likely that you (or the function that you’re calling) are trying to perform a binary operations with something that’s not a number.


    EDIT:

    Your mistake is in using [ rather than [[. Subsetting Day with [ will result in a list and not a numeric vector, since Day can be used as a list. [[ returns however an object of class of the item in the list.

    > Day <- Transaction(1, 2)["b"]
    > class(Day)
    [1] "list"
    > Day + 1
    Error in Day + 1 : non-numeric argument to binary operator
    
    > Day2 <- Transaction(1, 2)[["b"]]
    > class(Day2)
    [1] "numeric"
    > Day2 + 1
    [1] 3

    Transaction returns two vectors, as you have defined it. Day, as you can see, returns a list that contains one vector. Day2 is a vector.

    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.