我想知道有没有一种方法可以在 R 中的(来自包)上执行某种apply
功能来切割等
数组上的列?sparseMatrix
Matrix
k
并且是否仅将列中大于 0 的那些元素划分为组?
对于小sparseMatrix
代码看起来像这样,但我敢打赌它在更大的矩阵上不会有效。
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]"