title: Error in lis[[i]] : attempt to select less than one element
body: <p>This code is meant to compute the total distance of some given coordinates, but I don't know why it's not working. </p>
  
  <p>The error is: <code>Error in lis[[i]] : attempt to select less than one element.</code></p>
  
  <p>Here is the code:</p>
  
  <pre><code>distant&lt;-function(a,b)
  {
    return(sqrt((a[1]-b[1])^2+(a[2]-b[2])^2))
  }
  totdistance&lt;-function(lis)
  {
    totdis=0
    for(i in 1:length(lis)-1)
    {
      totdis=totdis+distant(lis[[i]],lis[[i+1]])
    }
    totdis=totdis+distant(lis[[1]],lis[[length(lis)]])
    return(totdis)
  }
  liss1&lt;-list()
  liss1[[1]]&lt;-c(12,12)
  liss1[[2]]&lt;-c(18,23)
  liss1[[4]]&lt;-c(29,25)
  liss1[[5]]&lt;-c(31,52)
  liss1[[3]]&lt;-c(24,21)
  liss1[[6]]&lt;-c(36,43)
  liss1[[7]]&lt;-c(37,14)
  liss1[[8]]&lt;-c(42,8)
  liss1[[9]]&lt;-c(51,47)
  liss1[[10]]&lt;-c(62,53)
  liss1[[11]]&lt;-c(63,19)
  liss1[[12]]&lt;-c(69,39)
  liss1[[13]]&lt;-c(81,7)
  liss1[[14]]&lt;-c(82,18)
  liss1[[15]]&lt;-c(83,40)
  liss1[[16]]&lt;-c(88,30)
  </code></pre>
  
  <p>Output:  </p>
  
  <pre><code>&gt; totdistance(liss1)
  Error in lis[[i]] : attempt to select less than one element
  &gt; distant(liss1[[2]],liss1[[3]])
  [1] 6.324555
  </code></pre>