Re: Matching where the source file has one record/ subject and the to-be-matched file has multiple records/subect

<[email protected]> Thu, 23 Jul 2026 18:33:35 -0400
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
As with so many explanations of requirements, they are incomplete enough to=
 make us guess.

And, if the goal is to use various packages, fine. Each should have enough =
documentation to see what it needs to work properly.

There is a straightforward solution to many problems like these ONCE YOU UN=
DERSTAND them. I don't.

But, sketching it out, you can probably do this in a loop of loops which mi=
ght suffice even if not fast or elegant.

It looks like reading in the files to df1 and df2

For all unique ID in df1
    For all unique ID in df2 (adjusted each time through the loop as it shr=
inks)
            Compare the current ID's using all relevant rows and generate a=
 score. Keep the best.
    Save info on the match chosen. Remove the relevant rows from both df1 a=
nd df2 as needed or mark them some way as done.

The catch here is the inner loop should not examine previously selected ite=
ms. One simple solution is to add a column (initially FALSE) to mark if a r=
ow has already been used. Adjust the conditions to only match ones set to F=
ALSE. Once any row is used, set it to TRUE.

As for how you determine if a match is to be used, it was not explained. So=
meone speculated it was based on the nearest date but who knows besides Joh=
n?



-----Original Message-----
From: R-help <[email protected]> On Behalf Of Bert Gunter
Sent: Thursday, July 23, 2026 1:32 PM
To: Sorkin, John <[email protected]>
Cc: r-help <[email protected]>
Subject: Re: [R] Matching where the source file has one record/ subject and=
 the to-be-matched file has multiple records/subect

If you have not found any of the responses (including Ravi's) sufficiently
helpful, I would suggest that you provide a reprex (which does *not*
require code for a solution).

Issues that seemed to me to require further clarification are:

1. After a subject's date in file 1 is matched to a date in file 2 and all
the records in file 2 for the matched subject of that date removed, do you
continue to seek matches for that file 1 date and remove further subjects
from file 2, or do you go on to the next record in file 1?

2. Can there be many identical dates in file 1 (this is related to point 1)
above)? Same question for file 2?

Apologies if this is just my stupidity and these points are obvious or
irrelevant.

Cheers,
Bert

On Wed, Jul 22, 2026 at 12:58=E2=80=AFPM Sorkin, John <[email protected]=
d.edu>
wrote:

> I am not showing code, because I don't know how to write the code. I know
> that MatchIt performs propensity score matching, but I don't know how to
> use the function to perform the match I want to conduct.
>
> I have two files
> File 1: a list of n  records, 1 record/subject. Each record has a subject
> number and a date
> File 2: a list of m records m>>n with multiple records/subject but each
> record has a distinct date. All records for a given subject in file 2 have
> the same subject number, but each record has a different date.
>
> I want to match each record from file 1 with a single record from file 2
> based on date. Once a record from file 2 is matched with a record from fi=
le
> 1, I want to remove all records from file 2 for the subject who was used =
in
> the match with the subject from file 1 so that the subject from file 2 wi=
ll
> not be used in any future match.
>
>
> Example
> file 1                                          file 2
> subject   date                              subject   date
> 1              01/01/2020                  100   01/01/2020
> 2              01/01/2021                  100   01/10/2021
> 3                                                   101  02/01/2021
>
> Subject 1 from file 1 is a perfect match for subject 100 from file2. The
> subjects are matched.
> All data from file 2 is removed for subject 100.
> Subject 2 from file 1 is then matched with subject 101 from file 2.
> If all records from subject 100 had not been removed from file2, subject 2
> would have been matched to subject 100 date 01/10/2021. I don't want this
> to happen. Once subject 100 is used to match subject 1, I don't want any
> other person from file 1 to be matched with subject 100.
>
> I hope you can help me with this, I suspect, not uncommon matching proble=
m!
>
> Thank you,
> John
>
> John David Sorkin M.D., Ph.D.
> Professor of Medicine, University of Maryland School of Medicine;
> Associate Director for Biostatistics and Informatics, Baltimore VA Medical
> Center Geriatrics Research, Education, and Clinical Center;
> Former PI Biostatistics and Informatics Core, University of Maryland
> School of Medicine Claude D. Pepper Older Americans Independence Center;
> Senior Statistician University of Maryland Center for Vascular Research;
>
> Division of Gerontology, Geriatrics and Palliative Medicine,
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> Cell phone 443-418-5382
>
>
>
> ______________________________________________
> [email protected] 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
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

______________________________________________
[email protected] 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.ht=
ml
and provide commented, minimal, self-contained, reproducible code.