mutate_meta: Modify object meta.data

View source: R/mutate-vdj.R

mutate_metaR Documentation

Modify object meta.data

Description

Modify object meta.data

Usage

mutate_meta(input, fn, ...)

Arguments

input

Single cell object or data.frame containing V(D)J data. If a data.frame is provided, the cell barcodes should be stored as row names.

fn

Function to use for modifying object meta.data. This can be either a function, e.g. mean, or a purrr-style lambda, e.g. ~ mean(.x, na.rm = TRUE) where ".x" refers to the meta.data table.

...

Additional arguments to pass to the provided function

Value

Object with mutated meta.data

Examples

# Sum two meta.data columns
# all additional arguments provided to mutate_meta() are passed directly to
# the function (in this case, dplyr::mutate())
res <- mutate_meta(
  tiny_sce,
  dplyr::mutate,
  NEW = nCount_RNA + nFeature_RNA
)

head(slot(res, "colData"), 1)

# Pass a purrr-style lambda
# this produces the same result as the previous example
res <- mutate_meta(
  tiny_sce,
  ~ dplyr::mutate(.x, NEW = nCount_RNA + nFeature_RNA)
)

head(slot(res, "colData"), 1)

# Modify multiple meta.data columns
res <- mutate_meta(
  tiny_sce,
  dplyr::mutate,
  NEW_1 = nCount_RNA + nFeature_RNA,
  NEW_2 = stringr::str_c(orig.ident, seurat_clusters)
)

head(slot(res, "colData"), 1)

# Remove meta.data columns
# any function can be passed to mutate_meta(), in this example
# dplyr::select() is used to remove columns
res <- mutate_meta(
  tiny_sce,
  dplyr::select,
  -UMAP_1
)

head(slot(res, "colData"), 1)

# Perform grouped operations using dplyr
# multi-line commands can be passed using brackets, just refer to the
# meta.data with '.x'
# this calculates the mean number of features for each group in the
# orig.ident meta.data column
res <- mutate_meta(tiny_sce, ~ {
  y <- dplyr::group_by(.x, orig.ident)
  y <- dplyr::mutate(y, mean_genes = mean(nFeature_RNA))
  y
})

head(slot(res, "colData"), 1)


rnabioco/djvdj documentation built on Oct. 24, 2023, 7:33 p.m.