Collapse counts based on multiple categorical metadata columns

collapse_counts(
  counts_mat,
  meta_data,
  varnames,
  min_cells_per_group = 0,
  keep_n = FALSE,
  how = c("sum", "mean")[1]
)

Arguments

counts_mat

counts matrix where columns represent cells and rows represent features

meta_data

data.frame containing cell metadata

varnames

subset of `meta_data` column names

min_cells_per_group

minimum cells to keep collapsed group

keep_n

keep or drop the `N` column containing the number of cells in each group. Default is `FALSE`

how

method of collapsing counts from groups. `sum` or `mean`

Examples

m <- matrix(sample.int(8, 100*500, replace=TRUE), nrow=100, ncol=500)
rownames(m) <- paste0("G", 1:100)
colnames(m) <- paste0("C", 1:500)
md1 <- sample(c("a", "b"), 500, replace=TRUE)
md2 <- sample(c("c", "d"), 500, replace=TRUE)
df <- data.frame(md1, md2)
data_collapsed <- collapse_counts(m, df, c("md1", "md2"))
head(data_collapsed$counts_mat)
#>    sample_2 sample_1 sample_0 sample_3
#> G1      505      588      531      530
#> G2      535      593      581      542
#> G3      537      623      524      552
#> G4      533      633      601      543
#> G5      520      604      567      531
#> G6      577      612      523      528