table_to_matrix: Switch table to matrix

View source: R/RcppExports.R

table_to_matrixR Documentation

Switch table to matrix

Description

Switch table to matrix

Usage

table_to_matrix(
  table,
  row_names = NULL,
  col_names = NULL,
  threshold = 0,
  return_sparse = FALSE
)

Arguments

table

A table with three columns: row, col, and value.

row_names

Character vector of row names to filter by.

col_names

Character vector of column names to filter by.

threshold

The threshold for filtering values based on absolute values. Defaults to 0.

return_sparse

Whether to return a sparse matrix. Defaults to false.

Value

A matrix.

Examples

table <- data.frame(
  row = c("r1", "r2", "r3", "r4", "r5", "r6"),
  col = c("c4", "c5", "c6", "c1", "c2", "c3"),
  value = c(0.6, -0.5, -0.4, 0.3, 0.2, 0.1)
)
matrix <- table_to_matrix(table)
table_new <- matrix_to_table(matrix)
identical(table, table_new)

table_to_matrix(table, threshold = 0.3)

table_to_matrix(
  table,
  row_names = c("r1", "r2"),
  col_names = c("c4", "c5")
)

sparse_matrix <- simulate_sparse_matrix(10, 10)
table_sparse <- matrix_to_table(
  sparse_matrix,
  keep_zero = TRUE
)
sparse_matrix_new <- table_to_matrix(
  table_sparse,
  return_sparse = TRUE
)
identical(sparse_matrix, sparse_matrix_new)

thisutils documentation built on Sept. 11, 2025, 5:12 p.m.