Skip to contents

Creates a new categorical column by recoding (collapsing) elements from an existing column. The mapping is stored in obj$meta$group_definitions for full reproducibility.

Usage

av_recode_groups(obj, source_col, new_col, mapping)

Arguments

obj

An AlphaVarSet object.

source_col

Character. Name of the source column to recode.

new_col

Character. Name of the new column to create.

mapping

A named list where names are the new group labels and values are character vectors of original elements to collapse. E.g., list(Regulatory = c("Promoter", "Enhancer"), Other = c("Intergenic")).

Value

The updated AlphaVarSet object with:

  • A new column new_col in $data.

  • The mapping stored in $meta$group_definitions[[new_col]].

Details

Values in source_col that are not covered by any entry in mapping are set to NA with a warning. This ensures only explicitly defined groups are included in downstream comparisons.

Examples

example_file <- system.file("extdata", "bcl11a_rs1427407_example.csv",
                            package = "AlphaVaR")
obj <- av_import_csv(example_file)
#> Reading bcl11a_rs1427407_example.csv ...

obj <- av_recode_groups(
  obj,
  source_col = "biosample_type",
  new_col = "sample_class",
  mapping = list(
    Cultured = c("cell_line", "in_vitro_differentiated_cells", "organoid"),
    Primary  = c("primary_cell", "tissue")
  )
)
table(obj$data$sample_class, useNA = "ifany")
#> 
#> Cultured  Primary     <NA> 
#>     1142     1530        4 
obj$meta$group_definitions$sample_class
#> $Cultured
#> [1] "cell_line"                     "in_vitro_differentiated_cells"
#> [3] "organoid"                     
#> 
#> $Primary
#> [1] "primary_cell" "tissue"      
#> 

obj <- av_compare_target(obj, target_col = "sample_class", target_value = "Primary")
#> Warning: Dropped 4 of 2676 rows with NA in 'sample_class', 'quantile_score'.
obj$stats$comparisons[["sample_class=Primary"]]$results_tidy
#> # A tibble: 1 × 12
#>   group   n_group n_rest median_group median_rest median_diff    p_val    p_adj
#>   <chr>     <int>  <int>        <dbl>       <dbl>       <dbl>    <dbl>    <dbl>
#> 1 Primary    1530   1142        0.465       0.529     -0.0633 0.000135 0.000135
#> # ℹ 4 more variables: effect_size <dbl>, effect_type <chr>, method <chr>,
#> #   significance_stars <chr>