• Register
1 vote
969 views

Problem :

I am very new to R. I am facing a issue while entering CurrentDay. I had previously created it as below :

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

The above PnL and Day are both numeric values.

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

The Transaction returned a as the list and b as the integer.

But my below code always gives following error:

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

Please guide me why am I receiving below error?

Error in fun(left, right) : non-numeric argument to binary operator.

 

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I had faced above issue in the scenario where for one of a method where I had passed a numeric value within quotes. Then I had done a lot of research on it. And I was able to find the solution on it. I am sharing the solution with you in form of below code example.

Code giving error as below:

corrplot(corr_data,order="hclust",addrect="3")

Error free code as below:

corrplot(corr_data,order="hclust",addrect=3)
I hope my above solution saved your lots of time.

 

8 4 1
5,680 points
0 votes

Solution:

Since your question is phrased concerning your error message and not whatever your function is attempting to achieve, 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 (view wikipedia for more). + is one such operator: "1 + 1" takes two operands (1 and 1) and produces another value (2). Note that the created value isn't necessarily diverse from the operands (e.g., 1 + 0 = 1).

R only knows how to execute + (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

At the time you view that error message, it implies that you are (or the function you're calling is) attempting to readact a binary operation with something that isn't a number.

EDIT:

Your error lies in the use of [ instead of [[. since Day is a list, subsetting with [ will return a list, not a numeric vector. [[, but, returns an object of the class of the item seized 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 identify it, returns a list of two vectors. Above, Day is a list comprise one vector. Day2, but, is easily a vector.

Error code:

corrplot(corr_data,order="hclust",addrect="3")

Correct one:

corrplot(corr_data,order="hclust",addrect=3)

 

10 6 4
31,120 points

Related questions

1 vote
1 answer 46 views
46 views
Problem: I&rsquo;m new in data frame. 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"] Anyone know why I am receiving this error?
asked Mar 10 Sabbir Hossain 14.6k points
0 votes
1 answer 1.2K views
0 votes
1 answer 2.6K views
2.6K views
Problem : I have been facing below error message and traceback: Error in FUN(X[[i]], ...) : only defined on the data frame with all numeric variables 5 stop("only defined on the data frame with all numeric variables") 4 FUN(X[[i]], ...) 3 lapply(args, ... .numeric(data[good,]) But now I get the different error message back as below : "Error: (list) object cannot be coerced to type 'double'"
asked Dec 16, 2019 alecxe 7.5k points
0 votes
1 answer 733 views
1 vote
1 answer 1.6K views
1.6K views
Problem : Currently I am learning the r programming. I am unable to find the answer to below error but I am unable to do so. Currently I have one dataframe it has a column of strings. My task is to count the exact number of characters present in each entry of ... , with no default Error in match.fun(FUN): argument "FUN" is missing, with no default Can someone guide me in fixing above error?
asked Mar 30, 2020 morrisBson 3.2k points
0 votes
1 answer 4 views
4 views
Problem: Error in family$linkinv(eta) : Argument eta must be a nonempty numeric vector.The reason the title of the question is the error I am getting is because I simply do not know how to interpret it.Can Anyone solve this problem?
asked Apr 11 sumaiya simi 23.4k points
0 votes
1 answer 3 views
3 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 23 shamimkhan 15.1k points
0 votes
1 answer 7 views
7 views
Problem: operator associativity is either left to right or
asked Feb 24 jyoti goyal 2.1k points
0 votes
1 answer 35 views
35 views
Problem: error in as.date.numeric(value) : 'origin' must be supplied.
asked Feb 16 charles mathews 5.5k points
0 votes
1 answer 322 views