John,

You may want to consider various ways to store and manipulate the data and
even switch between views as needed.

It sounds like one option is to have multiple tables. One table can be the
one with longitudinal data and can include multiple records/rows with the
same patient.id but another table can contain matches of patient.id and
control.id with perhaps some being NA at times.

When you want to, you can use various joins to make a combined table for
various uses. Joining the above tables on patient.id will populate the
result to include the same control.id for each relevant row if that is a
helpful scenario.

Obviously, this means some work to keep things synchronized when patients
are added or removed. 

Other ways to deal with things can involve adding additional columns for
special uses, such as to mark one row as being the primary for some purpose
or as having already been matched or other techniques that help refine later
analysis such as selecting a specific subset of rows.

And, although you have said you would prefer to use base R, you can consider
ways to do grouping in base R so that whatever actions you are doing will be
done piecewise one group at a time. The dplyr package can be very useful in
this regard as you can easily group things and then make changes or generate
reports based on it.

-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Sorkin, John
Sent: Thursday, September 18, 2025 7:08 AM
To: Leo Mada via R-help <r-help@r-project.org>
Subject: [R] Matching when each subject has multiple records, but each
subject should be used only once in the match

I have a file that contains longitudinal data for each subject. As a result,
each subject can have multiple records. For example a given subject might
have a record in Jan 2020, another in June 2020, another in Feb 2021,
another in May 2021, another in Sept 2022, etc. At each time for which a
subject has a record the subject is identified as a case or a control.

Over the course of the longitudinal data, I want to match a given case to a
given control. Once a subject is matched, I don't want the subject to be
eligible for being matched again.

If each subject had a single record, matching could easily be accomplished.
How can I accomplish the match in my file having repeated measures for each
subject?

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; 
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 and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to