Home > Software engineering >  results aren't stored properly, why?
results aren't stored properly, why?

Time:12-26

I don't get it why it doesn't store the results properly in the vector.

I only see the comment: numeric (empty), I've read about it on Stackflow and tried the solutions but can't fix my problem.

Is there a solution for it?

Would be very nice if somebody could help me out.

a <- c(100,101,102,100)
return1 <- diff(a)/a[-length(a)]


zz <- c()

for (i in return1){
  zz[i]<- 100*(i 1) 100
  
}

CodePudding user response:

EDIT: If you want to store values in zz, the index i in zz[i] should be an integer of length 1:length(return1).

Because return1 contains non-rounded numbers, this condition is not satisfied. You can solve around this by introducing a proper index that equals 1 in the first iteration and increases 1 unit with each subsequent iteration.

Hence, the following should work:

a <- c(100,101,102,100)
return1 <- diff(a)/a[-length(a)]

zz <- c()
count <- 0
for (i in return1){
  count <- count   1
  zz[count]<- 100*(i 1) 100
  
}
  •  Tags:  
  • r
  • Related