从R中的稀疏矩阵中提取稀疏行 [英] extract sparse rows from sparse matrix in r
本文介绍了从R中的稀疏矩阵中提取稀疏行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大的稀疏矩阵要在R中进行分析.例如:
I have a large sparse matrix to analyze in R. For instance:
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 行,作为稀疏向量. 如果我写
I want to extract the i-th row from this matrix, as a sparse vector. If I write
(x=A[1,])
我获得
[1] 0 7 0 0 0 0 0 0 0 0
但是我想要的是
[1] . 7 . . . . . . . .
我期望的是新矢量不会实现零.我该怎么办?
What I expect is that the new vector does not materialize the zeros. How can I do this?
谢谢
推荐答案
您可以使用drop = FALSE
:
A[1, , drop = FALSE]
# 1 x 10 sparse Matrix of class "dgCMatrix"
#
# [1,] . 7 . . . . . . . .
这篇关于从R中的稀疏矩阵中提取稀疏行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文