简体   繁体   English

从r中的稀疏矩阵中提取稀疏行

[英]extract sparse rows from sparse matrix in r

I have a large sparse matrix to analyze in R. For instance: 我有一个大的稀疏矩阵在R中进行分析。例如:

i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7)
(A <- sparseMatrix(i, j, x = x))
[1,] . 7 . . .  .  .  .  .  .
[2,] . . . . .  .  .  .  .  .
[3,] . . . . .  .  .  . 14  .
[4,] . . . . . 21  .  .  .  .
[5,] . . . . .  . 28  .  .  .
[6,] . . . . .  .  . 35  .  .
[7,] . . . . .  .  .  . 42  .
[8,] . . . . .  .  .  .  . 49

I want to extract the i -th row from this matrix, as a sparse vector. 我想从这个矩阵中提取第i行,作为稀疏向量。 If I write 如果我写

(x=A[1,])

I obtain 我知道了

 [1] 0 7 0 0 0 0 0 0 0 0

but what I would like is 但我想要的是

 [1] . 7 . . . . . . . .

What I expect is that the new vector does not materialize the zeros. 我期望的是新的向量不会实现零。 How can I do this? 我怎样才能做到这一点?

Thanks 谢谢

You can use drop = FALSE : 你可以使用drop = FALSE

A[1, , drop = FALSE]
# 1 x 10 sparse Matrix of class "dgCMatrix"
#                        
# [1,] . 7 . . . . . . . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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