Error in lis[[i]] : attempt to select less than one element

Let me reproduce your error in a simple way

>list1 = list()  
> list1[[0]]=list(a=c("a"))  
>Error in list1[[0]] = list(a = c("a")) : 
attempt to select less than one element

So, the next question is where are you accessing 0 index list ? (Indexing of lists starts with 1 in R )

As Molx, indicated in previous posts : “The : operator is evaluated before the subtraction – ” . This is causing 0 indexed list access.

For ex:

> 1:10-1  
[1] 0 1 2 3 4 5 6 7 8 9  
>1:(10-1)  
[1] 1 2 3 4 5 6 7 8 9

So replace the following lines of your code

>for(i in 1:(length(lis)-1))  
{     
     totdis=totdis+distant(lis[[i]],lis[[i+1]])  
}

Leave a Comment