title: How to compute relative difference in a circular domain (weekday) in R
body: <p>I would like to find the relative distance from a given day of week to another day of week. Assume the following R input of relative differences from day 0, in days:</p>
  
  <pre><code>day &lt;- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
  </code></pre>
  
  <p>The domain has a periodicity of one week (thus 7 days). I would like to get the following values:</p>
  
  <pre><code>relativedist &lt;- c(0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 3, 3, 2, 1, 0)
  </code></pre>
  
  <p>using modulo is a natural first guess, but as expected, not correct - it does not work for circular domains:</p>
  
  <pre><code>day%%4 
  </code></pre>
  
  <p>results in  </p>
  
  <pre><code>[1] 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2
  </code></pre>
  
  <p>I looked at packages <code>circular</code> and <code>circStats</code>, but failed to find an applicable function. </p>