Error: "argument to 'which' is not logical" for sparse logical matrix
Error: "argument to 'which' is not logical" for sparse logical matrix
这是我正在做的事情:
- 正在从文件加载稀疏矩阵。
- 正在提取具有此稀疏矩阵中值的索引(列、行)。
- 使用这些索引和值进行进一步计算。
当我在 R 命令提示符下执行这些步骤时,这工作正常。
但是当它在包的函数内完成时,第 2 步会抛出以下错误:
Error in which(matA != 0, arr.ind = TRUE) :
argument to 'which' is not logical
下面是带有示例的示例代码:
matA <- as(Matrix(c(0,1,2,1,0,0,3,0,2), nrow=3, ncol=3), "sparseMatrix") # Step 1
nz <- which(matA != 0, arr.ind = TRUE) # Step 2
> nz
row col
[1,] 2 1
[2,] 3 1
[3,] 1 2
[4,] 1 3
[5,] 3 3
在我的案例中加载的矩阵类型为:dsCMatrix、dgCMatrix。
class(matA != 0): lsCMatrix
我不明白为什么会导致错误。
请注意以下事项:
- 无法共享转储的稀疏矩阵文件。因此通过为步骤 1 创建虚拟矩阵显示了一个示例。
- 稀疏矩阵的维度很大。所以将稀疏矩阵转换为正则矩阵超出了内存限制
图书馆:
我正在使用的包提到了以下库:
Suggests:
testthat (>= 2.1.0),
knitr,
rmarkdown
Imports:
irlba,
text2vec,
dplyr,
magrittr,
Matrix,
readr,
rlang,
data.table,
stringr,
here
您需要加载库 Matrix
,可能是包没有加载它。请参阅下面的示例:
library(Seurat)
mat = pbmc_small@assays$RNA@counts
class(mat)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
which(mat>0)
Error in which(mat > 0) : argument to 'which' is not logical
library(Matrix)
head(which(mat>0,arr.ind=TRUE))
row col
CD79B 2 1
HLA-DQB1 6 1
LTB 9 1
SP100 12 1
CXCR4 23 1
CD3D 31 1
如果 Matrix 已经加载,可能是 Matrix::which 被某种方式屏蔽了。你可以这样做:
Matrix::which(mat>0)
这是我正在做的事情:
- 正在从文件加载稀疏矩阵。
- 正在提取具有此稀疏矩阵中值的索引(列、行)。
- 使用这些索引和值进行进一步计算。
当我在 R 命令提示符下执行这些步骤时,这工作正常。 但是当它在包的函数内完成时,第 2 步会抛出以下错误:
Error in which(matA != 0, arr.ind = TRUE) :
argument to 'which' is not logical
下面是带有示例的示例代码:
matA <- as(Matrix(c(0,1,2,1,0,0,3,0,2), nrow=3, ncol=3), "sparseMatrix") # Step 1
nz <- which(matA != 0, arr.ind = TRUE) # Step 2
> nz
row col
[1,] 2 1
[2,] 3 1
[3,] 1 2
[4,] 1 3
[5,] 3 3
在我的案例中加载的矩阵类型为:dsCMatrix、dgCMatrix。
class(matA != 0): lsCMatrix
我不明白为什么会导致错误。
请注意以下事项:
- 无法共享转储的稀疏矩阵文件。因此通过为步骤 1 创建虚拟矩阵显示了一个示例。
- 稀疏矩阵的维度很大。所以将稀疏矩阵转换为正则矩阵超出了内存限制
图书馆: 我正在使用的包提到了以下库:
Suggests:
testthat (>= 2.1.0),
knitr,
rmarkdown
Imports:
irlba,
text2vec,
dplyr,
magrittr,
Matrix,
readr,
rlang,
data.table,
stringr,
here
您需要加载库 Matrix
,可能是包没有加载它。请参阅下面的示例:
library(Seurat)
mat = pbmc_small@assays$RNA@counts
class(mat)
[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"
which(mat>0)
Error in which(mat > 0) : argument to 'which' is not logical
library(Matrix)
head(which(mat>0,arr.ind=TRUE))
row col
CD79B 2 1
HLA-DQB1 6 1
LTB 9 1
SP100 12 1
CXCR4 23 1
CD3D 31 1
如果 Matrix 已经加载,可能是 Matrix::which 被某种方式屏蔽了。你可以这样做:
Matrix::which(mat>0)