Skip to contents

Slice the sample or variable information tibble of an experiment().

These functions provide row-wise slicing operations similar to dplyr's slice functions. They select rows by position or based on values in specified columns, and update the expression matrix accordingly to match the new selection.

  • slice_obs() and slice_var(): Select rows by position

  • slice_head_obs() and slice_head_var(): Select first n rows

  • slice_tail_obs() and slice_tail_var(): Select last n rows

  • slice_sample_obs() and slice_sample_var(): Select random n rows

  • slice_max_obs() and slice_max_var(): Select rows with highest values

  • slice_min_obs() and slice_min_var(): Select rows with lowest values

Usage

slice_obs(exp, ...)

slice_var(exp, ...)

slice_head_obs(exp, n, prop)

slice_head_var(exp, n, prop)

slice_tail_obs(exp, n, prop)

slice_tail_var(exp, n, prop)

slice_sample_obs(exp, n, prop, weight_by = NULL, replace = FALSE)

slice_sample_var(exp, n, prop, weight_by = NULL, replace = FALSE)

slice_max_obs(exp, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)

slice_max_var(exp, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)

slice_min_obs(exp, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)

slice_min_var(exp, order_by, ..., n, prop, with_ties = TRUE, na_rm = FALSE)

Arguments

exp

An experiment().

...

<data-masking> For slice_*(), integer row positions. For slice_max() and slice_min(), variables to order by. Other arguments passed to the corresponding dplyr function.

n

For slice_head(), slice_tail(), slice_sample(), slice_max(), and slice_min(), the number of rows to select.

prop

For slice_head(), slice_tail(), slice_sample(), slice_max(), and slice_min(), the proportion of rows to select.

weight_by

For slice_sample(), sampling weights.

replace

For slice_sample(), should sampling be with replacement?

order_by

For slice_max() and slice_min(), variable to order by.

with_ties

For slice_max() and slice_min(), should ties be kept?

na_rm

For slice_max() and slice_min(), should missing values be removed?

Value

A new experiment() object.

Examples

# Create a toy experiment for demonstration  
exp <- toy_experiment()
# Add columns needed for demonstration
exp$sample_info$score <- c(10, 20, 30, 15, 25, 35)
exp$var_info$value <- c(5, 10, 15, 8)

# Select specific rows by position
slice_obs(exp, 1, 3, 5)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 3 samples, 4 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value

# Select first 3 samples
slice_head_obs(exp, n = 3)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 3 samples, 4 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value

# Select last 2 variables
slice_tail_var(exp, n = 2)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 6 samples, 2 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value

# Select 2 random samples
slice_sample_obs(exp, n = 2)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 2 samples, 4 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value

# Select samples with highest scores
slice_max_obs(exp, order_by = score, n = 2)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 2 samples, 4 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value

# Select variables with lowest values
slice_min_var(exp, order_by = value, n = 2)
#> 
#> ── Experiment ──────────────────────────────────────────────────────────────────
#>  Expression matrix: 6 samples, 2 variables
#>  Sample information fields: group, batch, and score
#>  Variable information fields: protein, peptide, glycan_composition, and value