简体   繁体   中英

R Matrix package: Demean sparse matrix

Is there a simple way to demean a sparse matrix by columns while considering zero-values as missing (using Matrix package)?

There seem to be two problems I struggle with:

Finding proper column means

Empty cells are considered zero rather than missing:

M0 <- matrix(rep(1:5,4),nrow = 4)
M0[2,2] <- M0[2,3] <- 0
M <- as(M0, "sparseMatrix")
M
#[1,] 1 5 4 3 2
#[2,] 2 . . 4 3
#[3,] 3 2 1 5 4
#[4,] 4 3 2 1 5
colMeans(M)
#[1] 2.50 2.50 1.75 3.25 3.50

Correct result should be:

colMeans_correct <- colSums(M) / c(4,3,3,4,4)
colMeans_correct
#[1] 2.500000 3.333333 2.333333 3.250000 3.500000

Subtract column mean

Subtraction is performed also on the missing cells:

sweep(M, 2, colMeans_correct)
#4 x 5 Matrix of class "dgeMatrix"
#     [,1]       [,2]       [,3]  [,4] [,5]
#[1,] -1.5  1.6666667  1.6666667 -0.25 -1.5
#[2,] -0.5 -3.3333333 -2.3333333  0.75 -0.5
#[3,]  0.5 -1.3333333 -1.3333333  1.75  0.5
#[4,]  1.5 -0.3333333 -0.3333333 -2.25  1.5

PS hope it is not a problem posting a question composed of two problems. They are connected to the same task and seem to reflect the same problem - distinguish between missing and actual zero values.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM