title: Convert cartesian angles to polar / compass / cardinal angles in R?
body: <p>This is a problem that I fought with for a bit and couldn't find a good answer out there. I did eventually solve it in R, but thought I would post it anyway in case others need it. If anyone has a more elegant solution, I would love to see it. This is a bit of a brute force effort.</p>
  
  <p>I have a series of paired XY (Cartesian) coordinates. I can easily get the angles between them using a simple atan() command. However, I want the angles in compass (polar? cardinal?) directions (where north=0°, east=90°, and so forth). Here's a minimal example to make the data and the Cartesian angles, and I've posted my brute force conversion to compass angle below. The degree conversion (from radians) uses deg() from the 'circular' package.</p>
  
  <pre><code>require(circular)
  test &lt;- data.frame(x=c(0,1,1,1,0,-1,-1,-1),y=c(1,1,0,-1,-1,-1,0,1))
  test$angle &lt;- deg(atan(test$y/test$x))
  test
  </code></pre>
  
  <p>...produces</p>
  
  <pre><code>   x  y angle
  1  0  1    90
  2  1  1    45
  3  1  0     0
  4  1 -1   -45
  5  0 -1   -90
  6 -1 -1    45
  7 -1  0     0
  8 -1  1   -45
  </code></pre>
  
  <p>Note that the angles into the lower-left and upper-left quadrants are the same as that into the lower- and upper-right quadrants, losing the directionality of the vectors.</p>