Simple for loop in R producing “replacement has length zero” in R

Arrays in R are 1-based.

Fibonacci[1]<-1
Fibonacci[2]<-1
Product<-1
for (i in 3:1000)
{

(the remainder as in your question)

The problem is Fibonacci[0] which is a 0-length numeric. When i = 2, this expression has a right hand side of numeric(0):

Fibonacci[i]<-(Fibonacci[i-1])+(Fibonacci[i-2])

Leave a Comment