title: Distance matrix for pairwise comparisons between two different vectors
body: <p>I am trying to compute a distance matrix between every pairwise comparison for vector j and vector k. I encountered one <a href="https://stackoverflow.com/questions/19654227/r-distance-matrix-build-from-two-vectors-n-1">stack overflow post related to this</a> but it compares each row of j to the corresponding row of k. However, I want to compare each row of j to every row of k and so on... Just like the dist() function in R will accomplish with one vector, however with two vectors.</p>
  <p>For example, dist() will take a vector of 100 units and return a lower triangle with 4950 comparisons/distances. I would like to achieve this except with a vector j of 100 units and a vector k of 100 units and compare each of j to each of k.</p>
  <p>A smaller example:</p>
  <pre><code>set.seed(5)
  a&lt;-rnorm(5)
  b&lt;-rnorm(5)
  cbind(a,b)
                 a          b
  [1,] -0.84085548 -0.6029080
  [2,]  1.38435934 -0.4721664
  [3,] -1.25549186 -0.6353713
  [4,]  0.07014277 -0.2857736
  [5,]  1.71144087  0.1381082
  </code></pre>
  <p>I would like to compare 1a to 1b, 2b, 3b, 4b, and 5b. Then, 2a to 1b, 2b, 3b, 4b, 5b. etc. Just like dist() except instead of a distance matrix with itself, a distance matrix with a different vector.</p>
  <p>Does anyone know what the best way to accomplish this might be?</p>
  <p>EDIT: In addition to Joel's great tidyverse answer, I figured out a sort of base R workaround to doing this...</p>
  <p>If you have a vector A of 100 items and a vector B of 100 items, you can concatenate a vector of A followed by B, use dist and then index only the columns and rows that comparisons between the two concatenated vectors:</p>
  <pre><code>distM&lt;-as.matrix(dist(c(A, B)))
  distM&lt;-as.matrix(distM[-(1:100),-(101:200)])
  </code></pre>