title: For-loop, calculate predicted movement, delete row if outside range
body: <p>I have a data-set containing vehicle movement, where vehicle time and position is logged with GPS. The issue is that there exist duplicate values (spoofing) for several of the vehicles, and I am not able to identify the true vehicle, outside the guess that the first time the vehicle is introduced it is the true vehicle. My intent is to create a fore-loop that calculates predicted movement from one position to the next , and if the next value is outside this value the row will be deleted. This will also remove outliers, where one position is extremely off for some reason.   </p>
  
  <pre><code>Dataset
  Vehicle ID      Time                 Lat     Long    Max Speed (kts)
  1             01.01.2013 12:00:00    9.535   18.536     20
  1             01.01.2013 12:10:00    9.539   18.539     20
  1             01.01.2013 12:20:00    65.535  35.545     20
  1             01.01.2013 12:30:00    65.835  35.545     20
  1             01.01.2013 12:40:00    9.541   18.542     20
  1             01.01.2013 12:50:00    66.135  35.536     20
  1             01.01.2013 13:00:00    9.543   18.545     20
  2             05.01.2013 17:00:00    13.535  15.536     30
  </code></pre>
  
  <p>The idea is to run a loop that takes the position from row 1 if the Vehicle ID = Vehicle Id + 1, and calculates the maximum distance possibly traveled by calculating the time between time and time for next row (time + 1) and multiple this by the maximum speed. Then calculate a maximum and minimum latitude and longitude for where the vehicle can be theoretically be at (time + 1) and if the position is outside these maximum values, the row will be deleted and the loop will run the same statement on next row. </p>
  
  <p>Something like this:</p>
  
  <pre><code>if vehicle ID = vehicle ID[n+1], 
  then (create latmax and latmin) ( time[n+1] - time ) * maximum speed +- latitude &amp;
  then (create lonmax and lonmin) ( time[n+1] - time ) * maximum speed +- longitude
  then if lat[n+1] &gt; latmax &amp; lat[n+1] &lt; latmin &amp; lon[n+1] &gt; lonmax &amp; lon[n+1] &lt; lonmax (deleterow) if not, do the same at next line
  </code></pre>
  
  <p>This should result in deleting row 3, 4 and 6 in my sample. For row 8 there is a new vehicle and a large deviation in position is allowed. </p>
  
  <p>This method does not take account in the fact that the earth is circular and distance between latitudes decrease the closer we get to the north or south pole. The best solution would take this into account by solving for this mathematically in the formula, or using distm or similar to calculate the true distance. Implementing cosinus in the formula is properly the easiest method. However the deviation between the outliers and the true position is normalt so large, that the curvature of the earth does not matter in practice for this data-set. </p>