title: Maximizing unique matched pairs without repeating entries
body: <p>I have a dataset of pairs of firms as demonstrated below:</p>
  
  <pre><code>    structure(list(year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 
  2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 
  2001, 2001), gvkey_treat = c(212340, 7435, 6008, 6008, 6008, 
  6008, 5169, 5169, 5169, 5169, 9850, 14477, 12142, 12142, 12142, 
  14590, 14590, 14590, 7912, 7912), gvkey.control = c(3170, 3170, 
  1632, 24800, 10115, 12389, 1632, 24800, 10115, 12389, 2146, 2146, 
  114524, 5074, 1891, 6403, 11228, 1661, 6403, 11228), pscoredif = c(0.00834195600000004, 
  0.007494632, 0.00505072699999998, 0.00736360899999999, 0.00615869899999999, 
  0.00785061100000001, 0.015638134, 0.013325252, 0.014530162, 0.01283825, 
  0.00707618099999996, 0.000622760999999972, 2.63809999999642e-05, 
  0.016737042, 0.00582589899999997, 0.004946012, 0.00147647300000003, 
  0.002811854, 0.00478919899999997, 0.00131966)), row.names = c(NA, 
  -20L), class = c("tbl_df", "tbl", "data.frame"))
  </code></pre>
  
  <p>I have a list of firms who are paired in a given year. I need to reduce this list in such a way that a firm only appears once in treat and once in control each year while also retaining the greatest possible number of firms. Secondarily, I would like to keep the matched pairs with the smallest pscoredif value if multiple options are available. </p>
  
  <pre><code>year gvkey_treat gvkey.control pscoredif
  2000    7435    3170    0.007494632
  2000    6008    1632    0.005050727
  2000    5169    24800   0.013325252
  2000    9850    2146    0.007076181
  2000    12142   114524  0.000026381
  2001    14590   11228   0.001476473
  2001    7912    6403    0.004789199
  </code></pre>
  
  <p>If someone could help explain how to do this in R it would be greatly appreciated!</p>