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)