title: Sequential subtraction for each observation against all other observations
body: <p>Here's some example data for this problem, let's call this example dataset df:</p>
  
  <pre><code>     df$x  
  
     obs  x   
      1   2  
      2   4   
      3   8   
      4   16   
  </code></pre>
  
  <p>What I'd like to accomplish is to find the difference for each x against all other xs for all x and store those differences in an object.</p>
  
  <p>Here's what the math should look like:</p>
  
  <pre><code>some code later:
  result_df
  
         obs  col1 col2 col3   
          1   2-4  2-8  2-16
          2   4-2  4-8  4-16
          3   8-2  8-4  8-16   
          4   16-2 16-4 16-8
  </code></pre>
  
  <p>What I've tried doing so far is something like this:</p>
  
  <pre><code>for (i in 1:nrow(df)){
  #Create empty matrix
  mat&lt;-matrix(rep(0,(nrow(df))*(nrow(df)-1)),ncol=nrow(df)-1,nrow=nrow(df))
  #occupy matrix with desired results
  mat[i,]&lt;-df$x[i]-df$x[i+1]
  print(mat)
  }
  </code></pre>
  
  <p>I intended this code to be valid for the first row of my result_df in my example (I guess I would have to create another loop to do it for each given x?), but it doesn't give me the desired results.</p>
  
  <p>I hope that's clear. Thanks! It'd be great to me able to do this in base R so I get a better grasp of it.</p>