Learn R Programming

thisutils (version 0.0.7)

as_matrix: Convert sparse matrix into dense matrix

Description

Convert sparse matrix into dense matrix

Usage

as_matrix(x, parallel = FALSE, sparse = FALSE)

Value

A dense or sparse matrix

Arguments

x

A matrix.

parallel

Logical value, default is FALSE. Setting to parallelize the computation with RcppParallel::setThreadOptions.

sparse

Logical value, default is FALSE, whether to output a sparse matrix.

Examples

Run this code
m <- simulate_sparse_matrix(
  1000, 1000,
  decimal = 3
)

system.time(
  a <- as.matrix(m)
)
system.time(
  b <- as_matrix(m)
)
system.time(
  c <- as_matrix(m, parallel = TRUE)
)
system.time(
  d <- as_matrix(m, sparse = TRUE)
)

m[1:5, 1:5]
a[1:5, 1:5]
b[1:5, 1:5]
c[1:5, 1:5]

identical(a, b)
identical(a, c)
identical(b, c)
identical(a, d)
identical(b, d)

Run the code above in your browser using DataLab