I am wondering is there a way to perform some kind of apply
function on a sparseMatrix
(from Matrix
package) in R to cut Columns on k
equinumerous groups?
And is there away to divide for groups only those elements in column that are greater than 0?
For small sparseMatrix
code looks like this but I bet it won't work efficient on bigger matrix.
library(Matrix)
i <- c(1:8, rep(8,7)); j <- c(1:8, 1:7); x <- c(8 * (1:8),1:7)
(A <- sparseMatrix(i, j, x = x))
#8 x 8 sparse Matrix of class "dgCMatrix"
[1,] 8 . . . . . . .
[2,] . 16 . . . . . .
[3,] . . 24 . . . . .
[4,] . . . 32 . . . .
[5,] . . . . 40 . . .
[6,] . . . . . 48 . .
[7,] . . . . . . 56 .
[8,] 1 2 3 4 5 6 7 64
>
"
> k<- 2
> apply(A,2,function(element){
+ cut(element,
+ k)})
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] "(4,8.01]" "(-0.016,8]" "(-0.024,12]" "(-0.032,16]" "(-0.04,20]" "(-0.048,24]" "(-0.056,28]" "(-0.064,32]"
[2,] "(-0.008,4]" "(8,16]" "(-0.024,12]" "(-0.032,16]" "(-0.04,20]" "(-0.048,24]" "(-0.056,28]" "(-0.064,32]"
[3,] "(-0.008,4]" "(-0.016,8]" "(12,24]" "(-0.032,16]" "(-0.04,20]" "(-0.048,24]" "(-0.056,28]" "(-0.064,32]"
[4,] "(-0.008,4]" "(-0.016,8]" "(-0.024,12]" "(16,32]" "(-0.04,20]" "(-0.048,24]" "(-0.056,28]" "(-0.064,32]"
[5,] "(-0.008,4]" "(-0.016,8]" "(-0.024,12]" "(-0.032,16]" "(20,40]" "(-0.048,24]" "(-0.056,28]" "(-0.064,32]"
[6,] "(-0.008,4]" "(-0.016,8]" "(-0.024,12]" "(-0.032,16]" "(-0.04,20]" "(24,48]" "(-0.056,28]" "(-0.064,32]"
[7,] "(-0.008,4]" "(-0.016,8]" "(-0.024,12]" "(-0.032,16]" "(-0.04,20]" "(-0.048,24]" "(28,56.1]" "(-0.064,32]"
[8,] "(-0.008,4]" "(-0.016,8]" "(-0.024,12]" "(-0.032,16]" "(-0.04,20]" "(-0.048,24]" "(-0.056,28]" "(32,64.1]"
Three possible approaches:
data.table
simple_triplet_matrix
and use therollup
function from theslam
package.vapply
Options 1 and 3 support parallel processing over columns. Option 3 has the fewest dependencies. An implementation of option 3 is available as part of the
quminorm
package. If I find time I might spin it off into a separate package in the future. Note that for functions that also require the zero values the best approach is to use functioncolapply_simple_triplet_matrix
from packageslam
.Here is a vignette comparing a variety of different schemes in terms of speed and memory consumption.