Skip to contents

A LIMS sample report lists one row per physical aliquot: every tube, swab, culture, extract and isolate has its own barcode. That is the right level for finding a sample in the freezer, but not for most questions about a cohort, such as how many perirectal swabs did each subject give? or on how many days was each subject sampled? lims_aggregate() combines one or more reports into a clean table of aliquots, and summarizes it at the level you need.

The example reports

lims2sra ships subsets of two LabVantage “IDSampleByStudy” exports (see ?lims_example):

  • ID_GOL_Microbiome: axilla, groin, perirectal and stool swabs from 12 subjects, sampled on 5 to 9 days each. Primary specimens only.
  • ID_FACEMDRO: five subjects from a study of patients with a history of multidrug-resistant organism (MDRO) infection and their hospital rooms: stool, rectal, perirectal, throat, saliva and environmental swabs, the bacterial cultures grown from them, and isolates, some with their species.
library(lims2sra)

reports <- lims_example()
basename(reports)
#> [1] "IDSampleByStudy_GOL_Microbiome_subset.xlsx"
#> [2] "IDSampleByStudy_FACEMDRO_subset.xlsx"

Reading reports

Give lims_aggregate() a path, a vector of paths, a directory of reports, a data frame you have already read, or a list of any of these. Excel (.xlsx, .xls), CSV and tab-delimited files are read.

aliquots <- lims_aggregate(reports)
aliquots
#> # A tibble: 2,135 × 19
#>    specimen_id    study       subject_id sample_type        aliquot_type     
#>    <chr>          <chr>       <chr>      <chr>              <chr>            
#>  1 S-260203-05794 ID_FACEMDRO E001       Environment        Environment      
#>  2 S-260203-05795 ID_FACEMDRO E001       Environment        Environment      
#>  3 S-260203-05806 ID_FACEMDRO E001       Environment        Environment      
#>  4 S-260203-05807 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>  5 S-260203-05808 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>  6 S-260203-05809 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>  7 S-260203-05810 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>  8 S-260203-05811 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>  9 S-260203-05812 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#> 10 S-260203-05813 ID_FACEMDRO E001       Environmental Swab Bacterial Culture
#>    parent_id      collection_date microbial_species
#>    <chr>          <date>          <chr>            
#>  1 NA             2026-01-12      NA               
#>  2 NA             2026-01-12      NA               
#>  3 NA             2026-01-12      NA               
#>  4 S-260203-05796 2026-01-12      NA               
#>  5 S-260203-05796 2026-01-12      NA               
#>  6 S-260203-05796 2026-01-12      NA               
#>  7 S-260203-05796 2026-01-12      NA               
#>  8 S-260203-05797 2026-01-12      NA               
#>  9 S-260203-05797 2026-01-12      NA               
#> 10 S-260203-05797 2026-01-12      NA               
#>    collection_event                       visit subject_day report   identifier
#>    <chr>                                  <int>       <dbl> <chr>    <chr>     
#>  1 E001 | Environment | 2026-01-12            1           0 facemdro E2        
#>  2 E001 | Environment | 2026-01-12            1           0 facemdro E3        
#>  3 E001 | Environment | 2026-01-12            1           0 facemdro E1        
#>  4 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES1-MRSA  
#>  5 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES1-ESBL  
#>  6 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES1-CRE   
#>  7 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES1-VRE   
#>  8 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES2-MRSA  
#>  9 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES2-ESBL  
#> 10 E001 | Environmental Swab | 2026-01-12     1           0 facemdro ES2-CRE   
#>    container sample_source tissue department         status        
#>    <chr>     <chr>         <lgl>  <chr>              <chr>         
#>  1 Zymo Swab NA            NA     Infectious Disease In Circulation
#>  2 Zymo Swab NA            NA     Infectious Disease In Circulation
#>  3 Zymo Swab NA            NA     Infectious Disease In Circulation
#>  4 NA        NA            NA     Infectious Disease In Circulation
#>  5 NA        NA            NA     Infectious Disease In Circulation
#>  6 NA        NA            NA     Infectious Disease In Circulation
#>  7 NA        NA            NA     Infectious Disease In Circulation
#>  8 NA        NA            NA     Infectious Disease In Circulation
#>  9 NA        NA            NA     Infectious Disease In Circulation
#> 10 NA        NA            NA     Infectious Disease In Circulation
#>    disposal_status
#>    <chr>          
#>  1 NA             
#>  2 NA             
#>  3 NA             
#>  4 NA             
#>  5 NA             
#>  6 NA             
#>  7 NA             
#>  8 NA             
#>  9 NA             
#> 10 NA             
#> # ℹ 2,125 more rows

The aliquot table starts with standard columns, whatever your LIMS calls them:

column from the report
specimen_id Sample the aliquot barcode
study Study
subject_id External Participant ID
sample_type Original Sample Type, or else Sample Type the specimen collected
aliquot_type Sample Type what this aliquot is (e.g. a culture)
parent_id Immediate Parent the aliquot it was derived from
collection_date Collection Date a Date
microbial_species Microbial Species
collection_event subject, sample type and date
visit the subject’s 1st, 2nd, … collection date
subject_day days since the subject’s first collection
report the report the aliquot came from

followed by the reports’ other columns, with snake_case names. If your reports use other column names, set them with specimen_col, subject_col, study_col, type_col, original_type_col, date_col, species_col and parent_col; only the barcode column is required.

When reports are combined, an aliquot listed in more than one of them (e.g. in overlapping exports) is kept once, with a message; if the listings disagree, a warning names the barcodes.

Excel reports are read with column types guessed from every row, not just the first 1000 as readxl::read_excel() does by default. In the full FACEMDRO export, the first Microbial Species entry is on row 1020, and the default would silently read the whole column as empty.

Sample type, aliquot type and lineage

In FACEMDRO, most aliquots are not specimens but things grown from them: bacterial cultures, the isolates picked from those cultures, and cell pellets.

facemdro <- aliquots[aliquots$study == "ID_FACEMDRO", ]
sort(table(facemdro$aliquot_type), decreasing = TRUE)
#> 
#>  Bacterial Culture Bacterial Cell Iso        Environment Environmental Swab 
#>                887                250                120                120 
#>              Cells              Stool             Saliva        Throat Swab 
#>                 79                 48                 39                 39 
#>    Perirectal Swab        Rectal Swab         Stool Swab              Feces 
#>                 37                 37                 34                  9

A culture grown from a throat swab comes from the same collection as the swab itself. LabVantage records the specimen in “Original Sample Type”, and lims_aggregate() uses it as sample_type, keeping the report’s own “Sample Type” as aliquot_type. It also follows parent_id back to the specimen, which matters for isolates subcultured from other isolates, whose “Original Sample Type” is just “Bacterial Cell Iso”. Here is one isolate, the culture it was picked from, and the swab that was cultured:

lineage <- function(id) {
  out <- character()
  while (!is.na(id)) {
    out <- c(out, id)
    id <- aliquots$parent_id[match(id, aliquots$specimen_id)]
  }
  aliquots[match(out, aliquots$specimen_id),
           c("specimen_id", "aliquot_type", "sample_type", "parent_id",
             "collection_date")]
}
isolates <- facemdro[facemdro$aliquot_type == "Bacterial Cell Iso" &
                       !is.na(facemdro$parent_id), ]
lineage(isolates$specimen_id[1])
#> # A tibble: 3 × 5
#>   specimen_id    aliquot_type       sample_type parent_id      collection_date
#>   <chr>          <chr>              <chr>       <chr>          <date>         
#> 1 S-260206-03322 Bacterial Cell Iso Stool Swab  S-260203-05824 2026-01-12     
#> 2 S-260203-05824 Bacterial Culture  Stool Swab  S-260203-05804 2026-01-12     
#> 3 S-260203-05804 Stool Swab         Stool Swab  NA             2026-01-12

All three have the same sample_type, subject and collection date, so they belong to one collection event. Set use_original_type = FALSE to use each aliquot’s own “Sample Type” instead.

A few isolates have no parent in the LIMS at all (their “Original Sample Type” is also “Bacterial Cell Iso”), so they keep “Bacterial Cell Iso” as their sample type, and form collection events of their own.

Levels

The by argument sets the level of the summary:

by one row per
"aliquot" (the default) barcode
"collection_event" subject, sample type and collection date
"subject" subject (within study)
"sample_type" sample type
"collection_date" collection date
"microbial_species" microbial species
"study" study

Each summary counts the distinct aliquots, collection events, subjects, sample types, collection dates and (when the reports record them) microbial species in each row, with its first and last collection dates.

lims_aggregate(reports, by = "study")
#> # A tibble: 2 × 9
#>   study             n_aliquots n_collection_events n_subjects n_sample_types
#>   <chr>                  <int>               <int>      <int>          <int>
#> 1 ID_FACEMDRO             1699                 294          5             10
#> 2 ID_GOL_Microbiome        436                 231         12              4
#>   n_collection_dates n_microbial_species first_collection_date
#>                <int>               <int> <date>               
#> 1                 55                   7 2024-08-02           
#> 2                 44                   0 2025-09-02           
#>   last_collection_date
#>   <date>              
#> 1 2026-01-16          
#> 2 2026-06-02

Collection events

A collection event is one specimen collected from one subject on one day: the unique combination of subject, sample type and collection date. It is usually what a clinical protocol calls a “sample”, however many aliquots, cultures or isolates it was split into.

events <- lims_aggregate(reports, by = "collection_event")
events
#> # A tibble: 525 × 13
#>    study       subject_id sample_type        collection_date
#>    <chr>       <chr>      <chr>              <date>         
#>  1 ID_FACEMDRO E001       Bacterial Cell Iso 2026-01-13     
#>  2 ID_FACEMDRO E001       Environment        2026-01-12     
#>  3 ID_FACEMDRO E001       Environment        2026-01-13     
#>  4 ID_FACEMDRO E001       Environment        2026-01-15     
#>  5 ID_FACEMDRO E001       Environment        2026-01-16     
#>  6 ID_FACEMDRO E001       Environmental Swab 2026-01-12     
#>  7 ID_FACEMDRO E001       Environmental Swab 2026-01-13     
#>  8 ID_FACEMDRO E001       Environmental Swab 2026-01-15     
#>  9 ID_FACEMDRO E001       Environmental Swab 2026-01-16     
#> 10 ID_FACEMDRO E001       Perirectal Swab    2026-01-12     
#>    collection_event                       n_aliquots n_collection_events
#>    <chr>                                       <int>               <int>
#>  1 E001 | Bacterial Cell Iso | 2026-01-13          1                   1
#>  2 E001 | Environment | 2026-01-12                 3                   1
#>  3 E001 | Environment | 2026-01-13                 3                   1
#>  4 E001 | Environment | 2026-01-15                 3                   1
#>  5 E001 | Environment | 2026-01-16                 3                   1
#>  6 E001 | Environmental Swab | 2026-01-12         15                   1
#>  7 E001 | Environmental Swab | 2026-01-13         15                   1
#>  8 E001 | Environmental Swab | 2026-01-15         15                   1
#>  9 E001 | Environmental Swab | 2026-01-16         15                   1
#> 10 E001 | Perirectal Swab | 2026-01-12             5                   1
#>    n_subjects n_sample_types n_collection_dates n_microbial_species
#>         <int>          <int>              <int>               <int>
#>  1          1              1                  1                   1
#>  2          1              1                  1                   0
#>  3          1              1                  1                   0
#>  4          1              1                  1                   0
#>  5          1              1                  1                   0
#>  6          1              1                  1                   0
#>  7          1              1                  1                   0
#>  8          1              1                  1                   0
#>  9          1              1                  1                   0
#> 10          1              1                  1                   0
#>    first_collection_date last_collection_date
#>    <date>                <date>              
#>  1 2026-01-13            2026-01-13          
#>  2 2026-01-12            2026-01-12          
#>  3 2026-01-13            2026-01-13          
#>  4 2026-01-15            2026-01-15          
#>  5 2026-01-16            2026-01-16          
#>  6 2026-01-12            2026-01-12          
#>  7 2026-01-13            2026-01-13          
#>  8 2026-01-15            2026-01-15          
#>  9 2026-01-16            2026-01-16          
#> 10 2026-01-12            2026-01-12          
#> # ℹ 515 more rows
tapply(events$n_aliquots, events$study, mean)
#>       ID_FACEMDRO ID_GOL_Microbiome 
#>          5.778912          1.887446

In GOL, most swabs were stored as two aliquots; in FACEMDRO, a single environmental swab can lead to dozens of cultures and isolates.

Subjects

lims_aggregate(lims_example("gol"), by = "subject")
#> # A tibble: 12 × 9
#>    study             subject_id n_aliquots n_collection_events n_subjects
#>    <chr>             <chr>           <int>               <int>      <int>
#>  1 ID_GOL_Microbiome GOL_001            23                  19          1
#>  2 ID_GOL_Microbiome GOL_007            13                  11          1
#>  3 ID_GOL_Microbiome GOL_014            52                  26          1
#>  4 ID_GOL_Microbiome GOL_015            54                  27          1
#>  5 ID_GOL_Microbiome GOL_016            42                  21          1
#>  6 ID_GOL_Microbiome GOL_017            36                  18          1
#>  7 ID_GOL_Microbiome GOL_018            26                  13          1
#>  8 ID_GOL_Microbiome GOL_019            42                  21          1
#>  9 ID_GOL_Microbiome GOL_020            48                  24          1
#> 10 ID_GOL_Microbiome GOL_021            36                  18          1
#>    n_sample_types n_collection_dates first_collection_date last_collection_date
#>             <int>              <int> <date>                <date>              
#>  1              4                  5 2025-09-02            2025-09-23          
#>  2              4                  5 2025-09-26            2025-10-14          
#>  3              4                  9 2026-01-22            2026-05-11          
#>  4              4                  9 2026-02-04            2026-05-17          
#>  5              4                  7 2026-02-10            2026-04-09          
#>  6              4                  5 2026-02-10            2026-04-03          
#>  7              4                  5 2026-02-18            2026-03-11          
#>  8              4                  7 2026-02-18            2026-05-09          
#>  9              4                  7 2026-03-04            2026-06-02          
#> 10              4                  5 2026-03-04            2026-04-15          
#> # ℹ 2 more rows

n_collection_dates is the number of visits; first_collection_date and last_collection_date give each subject’s follow-up.

Sample types and dates

lims_aggregate(lims_example("facemdro"), by = "sample_type")
#> # A tibble: 10 × 9
#>    sample_type        n_aliquots n_collection_events n_subjects n_sample_types
#>    <chr>                   <int>               <int>      <int>          <int>
#>  1 Bacterial Cell Iso         15                   8          5              1
#>  2 Environment               120                  40          5              1
#>  3 Environmental Swab        625                  40          5              1
#>  4 Feces                       9                   9          4              1
#>  5 Perirectal Swab           316                  37          5              1
#>  6 Rectal Swab                37                  37          5              1
#>  7 Saliva                     39                  39          5              1
#>  8 Stool                      48                  23          5              1
#>  9 Stool Swab                250                  23          5              1
#> 10 Throat Swab               240                  38          5              1
#>    n_collection_dates n_microbial_species first_collection_date
#>                 <int>               <int> <date>               
#>  1                  8                   6 2024-08-02           
#>  2                 40                   0 2024-08-20           
#>  3                 40                   1 2024-08-20           
#>  4                  9                   0 2024-08-29           
#>  5                 37                   1 2024-08-20           
#>  6                 37                   0 2024-08-20           
#>  7                 39                   0 2024-08-20           
#>  8                 23                   0 2024-08-22           
#>  9                 23                   0 2024-08-22           
#> 10                 38                   0 2024-08-20           
#>    last_collection_date
#>    <date>              
#>  1 2026-01-13          
#>  2 2026-01-16          
#>  3 2026-01-16          
#>  4 2025-11-11          
#>  5 2026-01-12          
#>  6 2026-01-12          
#>  7 2026-01-13          
#>  8 2026-01-15          
#>  9 2026-01-15          
#> 10 2026-01-13
lims_aggregate(lims_example("gol"), by = "collection_date")
#> # A tibble: 44 × 8
#>    collection_date n_aliquots n_collection_events n_subjects n_sample_types
#>    <date>               <int>               <int>      <int>          <int>
#>  1 2025-09-02               5                   4          1              4
#>  2 2025-09-10               5                   4          1              4
#>  3 2025-09-16               5                   4          1              4
#>  4 2025-09-17               5                   4          1              4
#>  5 2025-09-23               3                   3          1              3
#>  6 2025-09-26               3                   3          1              3
#>  7 2025-10-03               3                   3          1              3
#>  8 2025-10-04               2                   1          1              1
#>  9 2025-10-13               3                   3          1              3
#> 10 2025-10-14               2                   1          1              1
#>    n_collection_dates first_collection_date last_collection_date
#>                 <int> <date>                <date>              
#>  1                  1 2025-09-02            2025-09-02          
#>  2                  1 2025-09-10            2025-09-10          
#>  3                  1 2025-09-16            2025-09-16          
#>  4                  1 2025-09-17            2025-09-17          
#>  5                  1 2025-09-23            2025-09-23          
#>  6                  1 2025-09-26            2025-09-26          
#>  7                  1 2025-10-03            2025-10-03          
#>  8                  1 2025-10-04            2025-10-04          
#>  9                  1 2025-10-13            2025-10-13          
#> 10                  1 2025-10-14            2025-10-14          
#> # ℹ 34 more rows

Microbial species

FACEMDRO records the species of some isolates in “Microbial Species”:

species <- lims_aggregate(lims_example("facemdro"),
                          by = "microbial_species")
species
#> # A tibble: 8 × 9
#>   microbial_species            n_aliquots n_collection_events n_subjects
#>   <chr>                             <int>               <int>      <int>
#> 1 DUPLICATE                             3                   2          1
#> 2 Enterococcus faecalis                 1                   1          1
#> 3 Escherichia coli                      1                   1          1
#> 4 Proteus mirabilis                     3                   3          2
#> 5 Pseudomonas aeruginosa                4                   3          2
#> 6 Staphylococcus aureus                 1                   1          1
#> 7 Staphylococcus aureus (MRSA)          3                   3          2
#> 8 NA                                 1683                 289          5
#>   n_sample_types n_collection_dates n_microbial_species first_collection_date
#>            <int>              <int>               <int> <date>               
#> 1              1                  2                   1 2024-08-21           
#> 2              1                  1                   1 2025-05-17           
#> 3              1                  1                   1 2025-07-23           
#> 4              1                  3                   1 2024-08-02           
#> 5              2                  3                   1 2025-06-16           
#> 6              1                  1                   1 2025-06-13           
#> 7              1                  3                   1 2025-06-27           
#> 8             10                 52                   0 2024-08-02           
#>   last_collection_date
#>   <date>              
#> 1 2024-08-29          
#> 2 2025-05-17          
#> 3 2025-07-23          
#> 4 2025-07-27          
#> 5 2025-07-27          
#> 6 2025-06-13          
#> 7 2026-01-13          
#> 8 2026-01-16

Aliquots with no species form their own group (NA). Real LIMS data need checking: here “DUPLICATE” is a note typed into the species field, and “Staphylococcus aureus (MRSA)” might be counted with Staphylococcus aureus. Clean the aliquot table, and summarize it again (the aliquot table can be passed back to lims_aggregate()):

fac <- lims_aggregate(lims_example("facemdro"))
fac$microbial_species[fac$microbial_species == "DUPLICATE"] <- NA
fac$microbial_species <- sub(" \\(MRSA\\)$", "", fac$microbial_species)
lims_aggregate(fac, by = "microbial_species")
#> # A tibble: 6 × 9
#>   microbial_species      n_aliquots n_collection_events n_subjects
#>   <chr>                       <int>               <int>      <int>
#> 1 Enterococcus faecalis           1                   1          1
#> 2 Escherichia coli                1                   1          1
#> 3 Proteus mirabilis               3                   3          2
#> 4 Pseudomonas aeruginosa          4                   3          2
#> 5 Staphylococcus aureus           4                   4          3
#> 6 NA                           1686                 289          5
#>   n_sample_types n_collection_dates n_microbial_species first_collection_date
#>            <int>              <int>               <int> <date>               
#> 1              1                  1                   1 2025-05-17           
#> 2              1                  1                   1 2025-07-23           
#> 3              1                  3                   1 2024-08-02           
#> 4              2                  3                   1 2025-06-16           
#> 5              1                  4                   1 2025-06-13           
#> 6             10                 52                   0 2024-08-02           
#>   last_collection_date
#>   <date>              
#> 1 2025-05-17          
#> 2 2025-07-23          
#> 3 2025-07-27          
#> 4 2025-07-27          
#> 5 2026-01-13          
#> 6 2026-01-16

GOL records no species, so it cannot be summarized by species:

lims_aggregate(lims_example("gol"), by = "microbial_species")
#> Error:
#> ! Cannot aggregate by "microbial_species": no microbial species data (the report has no `Microbial Species`, or it is empty; set `species_col`).

Combining levels

by takes several levels at once, for one row per combination. For example, the collection events and aliquots of each sample type from each GOL subject:

lims_aggregate(lims_example("gol"), by = c("subject", "sample_type"))
#> # A tibble: 48 × 10
#>    study             subject_id sample_type     n_aliquots n_collection_events
#>    <chr>             <chr>      <chr>                <int>               <int>
#>  1 ID_GOL_Microbiome GOL_001    Axilla Swab              5                   5
#>  2 ID_GOL_Microbiome GOL_001    Groin Swab               5                   5
#>  3 ID_GOL_Microbiome GOL_001    Perirectal Swab          5                   5
#>  4 ID_GOL_Microbiome GOL_001    Stool Swab               8                   4
#>  5 ID_GOL_Microbiome GOL_007    Axilla Swab              3                   3
#>  6 ID_GOL_Microbiome GOL_007    Groin Swab               3                   3
#>  7 ID_GOL_Microbiome GOL_007    Perirectal Swab          3                   3
#>  8 ID_GOL_Microbiome GOL_007    Stool Swab               4                   2
#>  9 ID_GOL_Microbiome GOL_014    Axilla Swab             16                   8
#> 10 ID_GOL_Microbiome GOL_014    Groin Swab              16                   8
#>    n_subjects n_sample_types n_collection_dates first_collection_date
#>         <int>          <int>              <int> <date>               
#>  1          1              1                  5 2025-09-02           
#>  2          1              1                  5 2025-09-02           
#>  3          1              1                  5 2025-09-02           
#>  4          1              1                  4 2025-09-02           
#>  5          1              1                  3 2025-09-26           
#>  6          1              1                  3 2025-09-26           
#>  7          1              1                  3 2025-09-26           
#>  8          1              1                  2 2025-10-04           
#>  9          1              1                  8 2026-01-22           
#> 10          1              1                  8 2026-01-22           
#>    last_collection_date
#>    <date>              
#>  1 2025-09-23          
#>  2 2025-09-23          
#>  3 2025-09-23          
#>  4 2025-09-17          
#>  5 2025-10-13          
#>  6 2025-10-13          
#>  7 2025-10-13          
#>  8 2025-10-14          
#>  9 2026-05-11          
#> 10 2026-05-11          
#> # ℹ 38 more rows

or the specimens the FACEMDRO isolates of each species were grown from:

lims_aggregate(fac, by = c("microbial_species", "sample_type"))
#> # A tibble: 16 × 10
#>    microbial_species      sample_type        n_aliquots n_collection_events
#>    <chr>                  <chr>                   <int>               <int>
#>  1 Enterococcus faecalis  Bacterial Cell Iso          1                   1
#>  2 Escherichia coli       Bacterial Cell Iso          1                   1
#>  3 Proteus mirabilis      Bacterial Cell Iso          3                   3
#>  4 Pseudomonas aeruginosa Bacterial Cell Iso          3                   2
#>  5 Pseudomonas aeruginosa Environmental Swab          1                   1
#>  6 Staphylococcus aureus  Bacterial Cell Iso          4                   4
#>  7 NA                     Bacterial Cell Iso          3                   3
#>  8 NA                     Environment               120                  40
#>  9 NA                     Environmental Swab        624                  40
#> 10 NA                     Feces                       9                   9
#>    n_subjects n_sample_types n_collection_dates n_microbial_species
#>         <int>          <int>              <int>               <int>
#>  1          1              1                  1                   1
#>  2          1              1                  1                   1
#>  3          2              1                  3                   1
#>  4          1              1                  2                   1
#>  5          1              1                  1                   1
#>  6          3              1                  4                   1
#>  7          2              1                  3                   0
#>  8          5              1                 40                   0
#>  9          5              1                 40                   0
#> 10          4              1                  9                   0
#>    first_collection_date last_collection_date
#>    <date>                <date>              
#>  1 2025-05-17            2025-05-17          
#>  2 2025-07-23            2025-07-23          
#>  3 2024-08-02            2025-07-27          
#>  4 2025-06-27            2025-07-27          
#>  5 2025-06-16            2025-06-16          
#>  6 2025-06-13            2026-01-13          
#>  7 2024-08-02            2025-07-27          
#>  8 2024-08-20            2026-01-16          
#>  9 2024-08-20            2026-01-16          
#> 10 2024-08-29            2025-11-11          
#> # ℹ 6 more rows

by can also name any column of the aliquot table, such as aliquot_type or visit:

lims_aggregate(fac, by = c("sample_type", "aliquot_type"))
#> # A tibble: 22 × 10
#>    sample_type        aliquot_type       n_aliquots n_collection_events
#>    <chr>              <chr>                   <int>               <int>
#>  1 Bacterial Cell Iso Bacterial Cell Iso         15                   8
#>  2 Environment        Environment               120                  40
#>  3 Environmental Swab Bacterial Cell Iso         23                  11
#>  4 Environmental Swab Bacterial Culture         480                  40
#>  5 Environmental Swab Cells                       2                   2
#>  6 Environmental Swab Environmental Swab        120                  40
#>  7 Feces              Feces                       9                   9
#>  8 Perirectal Swab    Bacterial Cell Iso        103                  28
#>  9 Perirectal Swab    Bacterial Culture         148                  37
#> 10 Perirectal Swab    Cells                      28                  11
#>    n_subjects n_sample_types n_collection_dates n_microbial_species
#>         <int>          <int>              <int>               <int>
#>  1          5              1                  8                   5
#>  2          5              1                 40                   0
#>  3          3              1                 11                   1
#>  4          5              1                 40                   0
#>  5          1              1                  2                   0
#>  6          5              1                 40                   0
#>  7          4              1                  9                   0
#>  8          3              1                 28                   0
#>  9          5              1                 37                   0
#> 10          1              1                 11                   0
#>    first_collection_date last_collection_date
#>    <date>                <date>              
#>  1 2024-08-02            2026-01-13          
#>  2 2024-08-20            2026-01-16          
#>  3 2024-08-23            2025-06-20          
#>  4 2024-08-20            2026-01-16          
#>  5 2025-05-24            2025-05-30          
#>  6 2024-08-20            2026-01-16          
#>  7 2024-08-29            2025-11-11          
#>  8 2024-08-20            2025-06-22          
#>  9 2024-08-20            2026-01-12          
#> 10 2025-05-21            2025-06-02          
#> # ℹ 12 more rows

Saving the summaries

The results are ordinary tibbles; write them out with, for example, writexl::write_xlsx():

writexl::write_xlsx(
  list(aliquots = aliquots,
       collection_events = events,
       subjects = lims_aggregate(reports, by = "subject")),
  "cohort_summary.xlsx"
)

To see the cohort rather than tabulate it, see vignette("lims-timeline") and vignette("lims-heatmap").