title: Comparing xy coordinates in a dataframe
body: <p>I currently have a dataframe like this:</p>
  
  <pre><code>x        y       category
  159.5    143.5   1
  157.5    180.5   1
  127.5    159.5   1
  115.5    115.5   2
  179.5    101.5   2
  97.5     103.5   2
  149.5    397.5   3
  179.5    297.5   3
  </code></pre>
  
  <p>I'd like to compare each point against every other point in the dataframe. I want to get the difference in x and y (ie point 159.5, 143.5 to point 157.5,    180.5 would be a absolute difference of 2 in x and +37 in y)</p>
  
  <p>I've tried to do this in multiple ways but I'm not coming close, as well as using too many for-loops which is too slow. I'm positive there's a dplyr/function way of doing this and it would be really helpful.</p>
  
  <p>Here's my sample output goal (Not all filled out, but the general idea):</p>
  
  <pre><code>x        y        abs x-diff   y-diff  category            
  159.5    143.5    0            0       1         (from 159.5    143.5)    
  159.5    143.5    2            37      1         (from 157.5    180.5)
  159.5    143.5    32           16      1         (from 127.5    159.5)
  157.5    180.5    0            0       1         (from 157.5    180.5)
  157.5    180.5    2            -37     1         (from 159.5    143.5)
  157.5    180.5                         1
  127.5    159.5    0            0       1
  127.5    159.5                         1
  127.5    159.5                         1
  115.5    115.5    0           0        2         (from 115.5    115.5)
  115.5    115.5    64          -14      2         (from 179.5    101.5)
  115.5    115.5    18          -12      2         (from 97.5     103.5)
  179.5    101.5    0           0        2
  179.5    101.5                         2
  179.5    101.5                         2
  97.5     103.5    0           0        2
  97.5     103.5                         2
  97.5     103.5                         2
  149.5    397.5    0           0        3
  149.5    397.5                         3         
  179.5    297.5    0           0        3
  179.5    297.5                         3
  </code></pre>
  
  <p>There should of 3*3 (for cat. 1), 3*3 (for cat. 2) and 2*2 (for cat. 3), for a total of 22 rows. </p>
  
  <p>EDITED: I've added a category variable. I've tried to modify the previous answers to get it to work, but I would like to compare the coordinates within each category. The other answers don't work for this added layer since it repeats the entire data frame n times, but it's more complicated for a group_by.</p>