[R] Matching where the source file has one record/ subject and the to-be-matched file has multiple records/subect

Ravi Varadhan r@v|@v@r@dh@n @end|ng |rom jhu@edu
Thu Jul 23 20:48:45 CEST 2026


Here is a toy example I created many years ago on how to use the Hungarian algorithm in the "clue" package. In this example, the match will be exact since the two vectors are simply permuted, but they need not match exactly.  This answers Bert's concern.  An exact date match is not required. The algorithm will perform a nearest-neighbor match based on the absolute difference in days.

# Ravi Varadhan
# An example demonstrating the use of Hungarian algorithm for solving optimal matching
#
 require(clue)

 n <- 10
 x <- rt(n, df=3) + 1i * rt(n, df=3)  # this is set A
# y <- x[sample(n)]  # this is the vector to be permuted
 y <- rt(n, df=3) + 1i * rt(n, df=3)  # this is set B

 Cmat <- outer(x, y, FUN=function(x,y) Mod(x - y))  # Cost or Distance matrix between an element of A and B

 ans <- solve_LSAP(Cmat, maximum=FALSE)  # We are minimizing the linear sum

 dist <- function(x, y) sqrt(sum(Mod(x - y)^2))
 dist(x, y[c(ans)])

________________________________
From: Bert Gunter <bgunter.4567 using gmail.com>
Sent: Thursday, July 23, 2026 14:10
To: Ravi Varadhan <ravi.varadhan using jhu.edu>; R-help <R-help using r-project.org>; Sorkin, John <jsorkin using som.umaryland.edu>
Subject: Re: [R] Matching where the source file has one record/ subject and the to-be-matched file has multiple records/subect

You don't often get email from bgunter.4567 using gmail.com. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>

      External Email - Use Caution



... and related to Ravi's post, will there always be at least one date in file 2 that matches every date in file 1?  And if there isn't, what action should be taken?

-- Bert

On Thu, Jul 23, 2026 at 9:36 AM Ravi Varadhan via R-help <r-help using r-project.org<mailto:r-help using r-project.org>> wrote:
This is a classic matching problem, I believe.  Based on your description, you are looking for a 1-to-1 nearest-neighbor match based on the absolute difference in dates, with the constraint that once a subject in File 2 is used, all of their records are discarded.

Correct?

There are two classical approaches: greedy matching and global optimal matching.  While the greedy approach is simple to implement and often works, it can sometimes get into trouble (i.e., run out of good matches). The optimal approach would use the Hungarian algorithm (e.g., LSAP function in the clue package) to find the combination of matches that results in the lowest total date difference across the entire dataset.

Ravi

        [[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html<https://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list