title: Creating matched pairs in R
body: <p>I have a data set of ~20,000 cases and 3 possible controls for each case. Cases and controls are each uniquely identified by an ID variable. The possible controls have some overlap because they were joined with the cases in SQL in a manner that did not allow for matching without replacement. I have imported the data into R and am treating it as a group of ~20,000 cases and ~50,000 controls in order to select only 1 control for each case matched on the covariates (e.g., age) in the data set. My desired output is a case ID in column 1 and a matched control ID in column 2.</p>
  
  <p>I have been trying to use the MatchIt package to do the matching, but the output from the package (match.matrix) is a list of IDs that don't clearly correspond to the cases or controls. The package has a function called get_matches that seems like it would return the appropriate output, but the function arguments are opaque to me - I cannot figure out what <code>id_cols</code> and <code>getdata</code> are. There don't seem to be any tutorials on how to use MatchIt (or another package) to return just a list of the case IDs with their matched control IDs. I am using the Mahalanobis distance but don't care about the actual distance measure or returning a propensity score. What is the best package and approach for selecting just the single control who best matches each case, without replacement, and returning the case ID with the matched control ID?</p>
  
  <p>Sample imported data (note the overlap between some of the possible matches):</p>
  
  <pre><code>case_ID &lt;- c(1,1,1,2,2,2,3,3,3,4,4,4)
  control_ID &lt;- c(5,6,7,8,9,10,5,6,7,11,12,13)
  age &lt;- c(12,12,12,56,56,56,12,12,12,62,62,62)
  score &lt;- c(7,7,7,3,3,3,7,7,7,9,9,9)
  parity &lt;- c(1,1,1,4,4,4,1,1,1,2,2,2)
  retested &lt;- c(1,1,1,0,0,0,1,1,1,1,1,1)
  
  df &lt;- cbind(case_ID, control_ID, age, score, parity, retested)
  </code></pre>
  
  <p>Desired output (with covariates shown):</p>
  
  <pre><code>matched_case &lt;- c(1,2,3,4)
  matched_control &lt;- c(5,8,6,11)
  matched_age &lt;- c(12,56,12,62)
  matched_score &lt;- c(7,3,7,9)
  matched_parity &lt;- c(1,4,1,2)
  matched_retested &lt;- c(1,0,1,1)
  
  matched_df &lt;- cbind(matched_case, matched_control, matched_age, matched_score, matched_parity, matched_retested)
  </code></pre>