Skip to contents

Takes a named integer vector (sample names as names, integers as values) and returns a sparse 0/1 matrix where row names are sample names, column names are the unique integers, and entries indicate presence (1) or absence (0) of the integer value for each sample. This function is used in DEGAS screening.

Usage

Vec2sparse(x, col_prefix = "")

Arguments

x

A named integer vector. Names are treated as sample identifiers and must be unique. Values are coerced to factor levels to define columns.

col_prefix

A single character string. If supplied, it is prepended to every column name of the returned matrix (e.g. "V" produces "V1", "V2" …). The default "" keeps the original numeric levels unchanged.

Value

A sparse logical matrix of class "ngCMatrix" from the Matrix package. Rows correspond to samples, columns to unique values, with TRUE (1) indicating the sample carries that value.

Examples

if (FALSE) { # \dontrun{
v <- setNames(sample(1:5, 10, TRUE), paste0("Sample", 1:10))
mat1 <- Vec2sparse(v)              # original column names
mat2 <- Vec2sparse(v, col_prefix = "V")  # prefixed column names
print(mat1)
print(mat2)
} # }