downsample_count_vec: Downsample a vector of counts.

Description Usage Arguments Details Value Examples

View source: R/downsample.R

Description

Uniformly downsample a vector of non-negative integers to have a specified sum. That is, keep randomly subtracting 1 from nonzero elements of the vector until it has the desired sum. Each count is equally likely to be taken. That is, an element with value 8 is 4 times more likely to be decremented than an element with value 2.

Usage

1
2
3
4
5
6
7

Arguments

vec

A vector of non-negative integers.

end_sum

The number that you would like vector to sum to after downsampling. This must be less than the initial sum.

mat

A matrix of non-negative integers.

df

A data frame with gene names as columns.

Details

downsample_count_mat_rows() and downsample_count_mat_cols() just do downsample_vec() to all rows and columns of a matrix using apply().

downsample_gene_counts() downsamples on the subset of columns in the data frame that have names in get_gene_names().

If end_sum > sum(vec), vec is returned unchanged.

Value

A vector of non-negative integers.

Examples

1
2
3
4
5
6
7
8
downsample_count_vec(1:24, 24)
mat <- matrix(sample.int(100, size = 6^2, replace = TRUE), nrow = 6)
downsample_count_mat_rows(mat, end_sum = 6)
downsample_count_mat_cols(mat, end_sum = 6)
if (rlang::is_installed("mirmodels")) {
  ms_data <- mirmodels::get_ms_data(gene_predicate = ~ median(.) > 0)
  downsampled_ms <- downsample_gene_counts(ms_data, end_sum = 1e6)
}

mirvie/mirmisc documentation built on Dec. 21, 2021, 7 p.m.