我有一个常规矩阵(非稀疏),我想将其转换为sparseMatrix
(使用Matrix
包)。是否有一个功能可以做到这一点,或者我需要做一堆循环?
前任。
> regMat <- matrix(0, nrow=10, ncol=10)
> regMat[3,5] <- round(runif(1),2)*100
> regMat[2,8] <- round(runif(1),2)*100
> regMat[8,4] <- round(runif(1),2)*100
> regMat[1,6] <- round(runif(1),2)*100
> regMat[7,4] <- round(runif(1),2)*100
> regMat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 0 49 0 0 0 0
[2,] 0 0 0 0 0 0 0 93 0 0
[3,] 0 0 0 0 20 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 8 0 0 0 0 0 0
[8,] 0 0 0 14 0 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 0
有什么建议么?