Learn R Programming

thisutils (version 0.0.7)

simulate_sparse_matrix: Generate a simulated sparse matrix

Description

This function generates a sparse matrix with a specified number of rows and columns, a given sparsity level, and a distribution function for the non-zero values.

Usage

simulate_sparse_matrix(
  nrow,
  ncol,
  sparsity = 0.95,
  distribution_fun = function(n) stats::rpois(n, lambda = 0.5) + 1,
  decimal = 0,
  seed = 1
)

Value

A sparse matrix of class "dgCMatrix"

Arguments

nrow

Number of rows in the matrix.

ncol

Number of columns in the matrix.

sparsity

Proportion of zero elements (sparsity level). Default is 0.95, meaning 95% of elements are zero (5% are non-zero).

distribution_fun

Function to generate non-zero values.

decimal

Numeric value, default is 0. Controls the number of decimal places in the generated values. If set to 0, values will be integers. When decimal > 0, random decimal parts are uniformly distributed across the full range.

seed

Random seed for reproducibility.

Examples

Run this code
simulate_sparse_matrix(1000, 500) |>
  check_sparsity()

simulate_sparse_matrix(10, 10, decimal = 1)
simulate_sparse_matrix(10, 10, decimal = 5)

Run the code above in your browser using DataLab