这就是我在做什么:
当我在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
我不明白为什么这会导致错误。
请注意以下几点:
库: 我正在使用的程序包提及以下库:
Suggests:
testthat (>= 2.1.0),
knitr,
rmarkdown
Imports:
irlba,
text2vec,
dplyr,
magrittr,
Matrix,
readr,
rlang,
data.table,
stringr,
here
答案 0 :(得分:1)
您需要加载库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 :::。您可以这样做:
Matrix::which(mat>0)