R将矩阵或数据帧转换为sparseMatrix
我有一个常规矩阵(非稀疏矩阵),我想将其转换为R将矩阵或数据帧转换为sparseMatrix,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,我有一个常规矩阵(非稀疏矩阵),我想将其转换为sparseMatrix(使用matrix包)。是否有一个函数来执行此操作,或者我需要执行一系列循环 前 >regMat regMat[3,5]regMat[2,8]regMat[8,4]regMat[1,6]regMat[7,4]regMat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 0 0 0 0 49 0 0 0
sparseMatrix
(使用matrix
包)。是否有一个函数来执行此操作,或者我需要执行一系列循环
前
>regMat regMat[3,5]regMat[2,8]regMat[8,4]regMat[1,6]regMat[7,4]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
有什么建议吗?这里有两个选项:
library(Matrix)
A <- as(regMat, "sparseMatrix") # see also `vignette("Intro2Matrix")`
B <- Matrix(regMat, sparse = TRUE) # Thanks to Aaron for pointing this out
identical(A, B)
# [1] TRUE
A
# 10 x 10 sparse Matrix of class "dgCMatrix"
#
# [1,] . . . . . 45 . . . .
# [2,] . . . . . . . 59 . .
# [3,] . . . . 95 . . . . .
# [4,] . . . . . . . . . .
# [5,] . . . . . . . . . .
# [6,] . . . . . . . . . .
# [7,] . . . 23 . . . . . .
# [8,] . . . 63 . . . . . .
# [9,] . . . . . . . . . .
# [10,] . . . . . . . . . .
库(矩阵)
AJosh的回答很好,但这里有更多的选择和解释
吹毛求疵的“我有一个规则矩阵(非稀疏)…”实际上你有一个稀疏矩阵(大部分为0的矩阵);它只是未压缩的格式。您的目标是将其置于压缩存储格式中
稀疏矩阵可以压缩成多种存储格式。和是两种主要的格式as(regMat,“sparseMatrix”)
将矩阵转换为压缩的稀疏列类型dgCMatrix
。这通常是你想要的,但我更愿意明确地说出来
library(Matrix)
matCSC <- as(regMat, "dgCMatrix") # compressed sparse column CSC
matCSC
10 x 10 sparse Matrix of class "dgCMatrix"
[1,] . . . . . 57 . . . .
[2,] . . . . . . . 27 . .
[3,] . . . . 90 . . . . .
[4,] . . . . . . . . . .
[5,] . . . . . . . . . .
[6,] . . . . . . . . . .
[7,] . . . 91 . . . . . .
[8,] . . . 37 . . . . . .
[9,] . . . . . . . . . .
[10,] . . . . . . . . . .
matCSR <- as(regMat, "dgRMatrix") # compressed sparse row CSR
matCSR
10 x 10 sparse Matrix of class "dgRMatrix"
[1,] . . . . . 57 . . . .
[2,] . . . . . . . 27 . .
[3,] . . . . 90 . . . . .
[4,] . . . . . . . . . .
[5,] . . . . . . . . . .
[6,] . . . . . . . . . .
[7,] . . . 91 . . . . . .
[8,] . . . 37 . . . . . .
[9,] . . . . . . . . . .
[10,] . . . . . . . . . .
无耻插头-如果您感兴趣,我有更多。对于矩阵,有人已经有了答案
对于data.table,有一个包完成了这项工作
library(Matrix)
library(mltools)
x = data.table()
sparseM <- sparsify(x)
库(矩阵)
图书馆(mltools)
x=数据表()
sparseM对制作样本数据的小建议:regMat[cbind(c(3,2,8,1,7),c(5,8,4,6,4))]也Matrix(regMat,sparse=TRUE)
Thank@Aaron。我不知道这个习惯用法,但已经将它作为第二个选项添加到了答案中。@JoshO'Brien您知道是否有直接从文件转换的选项吗?因为密集矩阵可能太大,无法完全读入内存。谢谢作为对我问题的部分回答:
# Make data.frame of (row, column, value) triplets
df <- data.frame(
rowIdx = c(3,2,8,1,7),
colIdx = c(5,8,4,6,4),
val = round(runif(n = 5), 2) * 100
)
df
rowIdx colIdx val
1 3 5 90
2 2 8 27
3 8 4 37
4 1 6 57
5 7 4 91
# Build CSC matrix
matSparse <- sparseMatrix(
i = df$rowIdx,
j = df$colIdx,
x = df$val,
dims = c(10, 10)
)
matSparse
10 x 10 sparse Matrix of class "dgCMatrix"
[1,] . . . . . 57 . . . .
[2,] . . . . . . . 27 . .
[3,] . . . . 90 . . . . .
[4,] . . . . . . . . . .
[5,] . . . . . . . . . .
[6,] . . . . . . . . . .
[7,] . . . 91 . . . . . .
[8,] . . . 37 . . . . . .
[9,] . . . . . . . . . .
[10,] . . . . . . . . . .
library(Matrix)
library(mltools)
x = data.table()
sparseM <- sparsify(x)