From 2352b6df1a69cf6264b3132c60c77b332d8231d3 Mon Sep 17 00:00:00 2001 From: Eddie Ruiz <32622519+Ed2uiz@users.noreply.github.com> Date: Tue, 23 Jan 2024 11:37:10 -0500 Subject: [PATCH] feat: add as_ijx() convenience function --- R/utils.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/R/utils.R b/R/utils.R index b520df1..93498f9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -162,3 +162,22 @@ as_matrix <- function(x){ return(as.matrix(mat)) } } + +#' @title as_ijx +#' @param x dgCMatrix +#' @noRd +as_ijx <- function(x){ + # check that x is a dgCMatrix + if(!inherits(x = x, what = "dgCMatrix")){ + stop("Invalid input. Only dgCMatrix is currently supported.") + } + + # Convert dgc into TsparseMatrix class from {Matrix} + ijx <- as(x, "TsparseMatrix") + + # Get dbMatrix in triplet vector format (TSparseMatrix) + # Convert to 1-based indexing + df = data.frame(i = ijx@i + 1, j = ijx@j + 1, x = ijx@x) + + return(df) +}