0

I'm trying to write a binary function that returns 1 or 0 if the next period return is positive or negative. To do so, I tried with this code:

signal<- matrix(0, nrow = nrow(returns), ncol = ncol(returns))
for (i in 1:(ncol(returns))){
  for (t in 2:(nrow(returns)+1)){ 
    signal[1,i]<- 0 #Inizialize the signal binary function giving 0 to the first row
    if (returns[t,i] > returns[t-1,i]){ 
    signal[t,i] <- 1
    }else{
    signal[t,i]<- 0}
  }
}

but this gives me the error "if argument has zero length". I need this function to give a response (0,1) for all the times of each asset(columns)

Mattew
  • 1
  • [`site:stackoverflow.com "if argument has zero length"`](https://www.google.com/search?q=site%3Astackoverflow.com+"if+argument+has+zero+length"). – r2evans Dec 20 '21 at 15:53
  • Your `t` iterates one past the number of rows, so `returns[t,i]` should fail. – r2evans Dec 20 '21 at 15:55

0 Answers0