Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-numeric Argument to Binary Operator Error in R

Tags:

r

The issue I believe is how CurrentDay is entered. It was previously created as:

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

Both PnL and Day are numeric values.

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

Where Transaction returned a list and b is an integer.

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

Anyone know why I am receiving this error?

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

like image 284
BenLar Avatar asked Apr 16 '15 04:04

BenLar


People also ask

What is non-numeric argument in binary operator?

The “non-numeric argument to binary operator” error occurs when we perform arithmetic operations on non-numeric elements.

What is a binary operator in R?

Most of the operators that we use in R are binary operators (having two operands). Hence, they are infix operators, used between the operands. Actually, these operators do a function call in the background.

How do I convert non-numeric to numeric in R?

To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.


2 Answers

Because your question is phrased regarding your error message and not whatever your function is trying to accomplish, I will address the error.

- is the 'binary operator' your error is referencing, and either CurrentDay or MA (or both) are non-numeric.

A binary operation is a calculation that takes two values (operands) and produces another value (see wikipedia for more). + is one such operator: "1 + 1" takes two operands (1 and 1) and produces another value (2). Note that the produced value isn't necessarily different from the operands (e.g., 1 + 0 = 1).

R only knows 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 

When you see that error message, it means that you are (or the function you're calling is) trying to perform a binary operation with something that isn't a number.

EDIT:

Your error lies in the use of [ instead of [[. Because Day is a list, subsetting with [ will return a list, not a numeric vector. [[, however, returns an object of the class of the item contained 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, as you've defined it, returns a list of two vectors. Above, Day is a list contain one vector. Day2, however, is simply a vector.

like image 77
Richard Border Avatar answered Oct 13 '22 17:10

Richard Border


If you run this before your code, everything is gonna be OK.

'+' <- function(e1, e2) {   if (is.character(e1) | is.character(e2)) {     paste0(e1, e2)   } else {     base::`+`(e1, e2)   } } 
like image 39
Heaven Avatar answered Oct 13 '22 19:10

Heaven