
Summarize cell-level metadata to donor / patient level
SummarizeMetadata.RdGroups cells by one or more ID columns (e.g. donor ID, sample ID) and returns a wide data frame with one row per unique ID combination.
Usage
SummarizeMetadata(
data,
id_columns,
columns = NULL,
group.by = NULL,
numeric_func = mean,
numeric_na_rm = TRUE,
spread = NULL,
pivot = FALSE
)Arguments
- data
A Seurat object or a plain data frame / tibble.
- id_columns
Character vector of one or more column names that uniquely identify a donor or sample (e.g.
c("Donor.ID")orc("Donor.ID", "Visit")). One output row is produced per unique combination.- columns
Character vector of column names to include in the summary.
NULL(default) includes all columns exceptid_columns.id_columnsare always retained regardless of this setting.- group.by
Optional column name to aggregate the patient-level result up to a group level. When supplied, a two-stage aggregation is performed: (1) cells are collapsed to one row per
id_columnscombination (current behavior), then (2) those patient-level rows are averaged bygroup.by, giving each patient equal weight regardless of cell count. The returned data frame has one row pergroup.bylevel and gains ann_patientscolumn. DefaultNULL(single-stage).- numeric_func
Function to summarize numeric columns. Must accept a numeric vector and return a single value. Default
mean. Common alternatives:median,max,function(x) sd(x, na.rm = TRUE).- numeric_na_rm
Logical. Pass
na.rm = TRUEtonumeric_func. DefaultTRUE.- spread
Character or
NULL. When"sd"or"sem", adds a companion column for each numeric variable (e.g.Age_sdorAge_sem). At the patient level these are computed across cells; whengroup.byis also supplied they reflect the spread of patient means across patients in each group. DefaultNULL(no spread).- pivot
Logical. If
TRUE, the result is pivoted to long format (one row per ID × variable combination). DefaultFALSE(wide).
Details
Column handling:
- Numeric columns
Summarized with
numeric_func(default:mean).- Categorical columns - simple
When every cell in an ID group shares the same value (e.g. every cell from donor D1 has
Sex = "Male"), that single value is returned as-is.- Categorical columns - complex
When cells in an ID group have multiple values (e.g. a donor has several cell types), the result is a formatted string listing all values and their cell counts in descending order:
"CD4 T (n=1 420), NK (n=380), B (n=200)".
Examples
if (FALSE) { # \dontrun{
# One ID column
donor_df <- SummarizeMetadata(SeuratObj, id_columns = "Donor.ID")
# Multiple ID columns (one row per donor × visit combination)
donor_df <- SummarizeMetadata(SeuratObj,
id_columns = c("Donor.ID", "Visit"),
numeric_func = median)
# Long format for easy downstream plotting
long_df <- SummarizeMetadata(SeuratObj, id_columns = "Donor.ID", pivot = TRUE)
} # }