Skip to contents

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)
#> DataFrame with 1 row and 8 columns
#>                       orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.3
#>                      <character>  <numeric>    <integer>        <factor>
#> 1_AAGCCGCAGCTTATCG-1      avid_1          0            0               0
#>                      seurat_clusters    UMAP_1    UMAP_2       NEW
#>                             <factor> <numeric> <numeric> <numeric>
#> 1_AAGCCGCAGCTTATCG-1               0  -5.97705  -2.41811         0

# 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)
#> DataFrame with 1 row and 8 columns
#>                       orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.3
#>                      <character>  <numeric>    <integer>        <factor>
#> 1_AAGCCGCAGCTTATCG-1      avid_1          0            0               0
#>                      seurat_clusters    UMAP_1    UMAP_2       NEW
#>                             <factor> <numeric> <numeric> <numeric>
#> 1_AAGCCGCAGCTTATCG-1               0  -5.97705  -2.41811         0

# 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)
#> DataFrame with 1 row and 9 columns
#>                       orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.3
#>                      <character>  <numeric>    <integer>        <factor>
#> 1_AAGCCGCAGCTTATCG-1      avid_1          0            0               0
#>                      seurat_clusters    UMAP_1    UMAP_2     NEW_1
#>                             <factor> <numeric> <numeric> <numeric>
#> 1_AAGCCGCAGCTTATCG-1               0  -5.97705  -2.41811         0
#>                            NEW_2
#>                      <character>
#> 1_AAGCCGCAGCTTATCG-1     avid_10

# 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)
#> DataFrame with 1 row and 6 columns
#>                       orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.3
#>                      <character>  <numeric>    <integer>        <factor>
#> 1_AAGCCGCAGCTTATCG-1      avid_1          0            0               0
#>                      seurat_clusters    UMAP_2
#>                             <factor> <numeric>
#> 1_AAGCCGCAGCTTATCG-1               0  -2.41811

# 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)
#> DataFrame with 1 row and 8 columns
#>                       orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.3
#>                      <character>  <numeric>    <integer>        <factor>
#> 1_AAGCCGCAGCTTATCG-1      avid_1          0            0               0
#>                      seurat_clusters    UMAP_1    UMAP_2 mean_genes
#>                             <factor> <numeric> <numeric>  <numeric>
#> 1_AAGCCGCAGCTTATCG-1               0  -5.97705  -2.41811    2.35417