Most Mature Sparse Matrix Package for R

Most mature sparse matrix package for R?

Matrix is the most common and has also just been accepted R standard installation (as of 2.9.0), so should be broadly available.

Matrix in base:
https://stat.ethz.ch/pipermail/r-announce/2009/000499.html

Using sparse matrix as an input to ranger package in R

Since Version 0.7.2, sparse matrices like the ones from the package Matrix can now be passed to ranger, see the discussion here. Extending to what is said in the thread, sparse matrices are now also supported in the CRAN version and do not need additional parameters like in the inital github version.

R Matrix package: Demean sparse matrix

One option is to divide the colSums by the colSums of the non-zero logical matrix

colSums(M)/colSums(M!=0)
#[1] 2.500000 3.333333 2.333333 3.250000 3.500000

Or another option is to replace the 0 with NA and get the colMeans with na.rm = TRUE argument

colMeans(M*NA^!M, na.rm = TRUE)
#[1] 2.500000 3.333333 2.333333 3.250000 3.500000

Or as @user20650 commented

colSums(M) / diff(M@p)
#[1] 2.500000 3.333333 2.333333 3.250000 3.500000

where the 'p' is a pointer mentioned in the ?sparseMatrix

In typical usage, p is missing, i and j are vectors of positive
integers and x is a numeric vector. These three vectors, which must
have the same length, form the triplet representation of the sparse
matrix.

If i or j is missing then p must be a non-decreasing integer vector
whose first element is zero. It provides the compressed, or “pointer”
representation of the row or column indices, whichever is missing. The
expanded form of p, rep(seq_along(dp),dp) where dp <- diff(p), is used
as the (1-based) row or column indices.

Efficient way to check a large sparse matrix for non-finite values

If we want to know if a sparse matrix A has any Inf, -Inf, NaN or NA, we can do

any(!is.finite(A@x))
#[1] TRUE

If we also want to know their positions, we can do

subset(summary(A), !is.finite(x))
i j x
1 1 1 Inf
2 2 1 -Inf
3 1 3 NA

Remark:

See R: element-wise matrix division for distinctions between is.infinite, !is.finite, is.na and is.nan.

Assign sparse values to the diagonal of a sparse Matrix in R

I assume you have indeed a matrix of class ddiMatrix and want to create an empty matrix:

m1 <- as(m, "generalMatrix")
m1@i <- integer(0)
m1@p <- m1@p * 0L
m1@x <- numeric(0)
#10 x 10 sparse Matrix of class "dgCMatrix"
#
# [1,] . . . . . . . . . .
# [2,] . . . . . . . . . .
# [3,] . . . . . . . . . .
# [4,] . . . . . . . . . .
# [5,] . . . . . . . . . .
# [6,] . . . . . . . . . .
# [7,] . . . . . . . . . .
# [8,] . . . . . . . . . .
# [9,] . . . . . . . . . .
#[10,] . . . . . . . . . .

Of course, you could also simply construct a new empty sparse matrix of these dimensions.

If you have a CsparseMatrix with off-diagonal values, coerce it to a TsparseMatrix and remove all i, j, x values where i == j (or skip the coercion and calculate the correct indices from i and p which I find difficult).

m1 <- as(m, "generalMatrix")

m2 <- as(m1, "TsparseMatrix")

del <- m2@i == m2@j
m2@i <- m2@i[!del]
m2@j <- m2@j[!del]
m2@x <- m2@x[!del]


Related Topics



Leave a reply



Submit