对于{matrix}中的稀疏矩阵,是否有使用R function duplicated()的方法?

对于{matrix}中的稀疏矩阵,是否有使用R function duplicated()的方法?,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,有没有一种简单的方法可以为duplicated编写一个方法来获取dgCMatrix类?下面是一个小示例,所需的输出为 的复制(如矩阵(A),边距=2)) #分配示例 图书馆(矩阵) 我的想法是将这个稀疏矩阵还原为一个列表,RowLst或ColLst,这样RowLst[[i]]或ColLst[[i]]就是第i行或第i列的压缩向量。然后在此列表上应用重复的 duplicated.dgCMatrix <- function (dgCMat, MARGIN, include.all.zero.ve

有没有一种简单的方法可以为
duplicated
编写一个方法来获取
dgCMatrix
类?下面是一个小示例,所需的输出为
的复制(如矩阵(A),边距=2))

#分配示例
图书馆(矩阵)

我的想法是将这个稀疏矩阵还原为一个列表,
RowLst
ColLst
,这样
RowLst[[i]]
ColLst[[i]]
就是第i行或第i列的压缩向量。然后在此列表上应用重复的

duplicated.dgCMatrix <- function (dgCMat, MARGIN, include.all.zero.vectors = TRUE) {
  MARGIN <- as.integer(MARGIN)
  J <- rep(1:ncol(dgCMat), diff(dgCMat@p))
  I <- dgCMat@i + 1
  x <- dgCMat@x
  if (MARGIN == 1L) {
    ## check duplicated rows
    names(x) <- J
    if (include.all.zero.vectors) {
      RowLst <- split(x, factor(I, levels = 1:nrow(dgCMat)))
      } else {
      RowLst <- split(x, I)  ## will do `factor(I)` internally in `split`
      }
    result <- duplicated.default(RowLst)
    } else if (MARGIN == 2L) {
    ## check duplicated columns
    names(x) <- I
    if (include.all.zero.vectors) {
      ColLst <- split(x, factor(J, levels = 1:ncol(dgCMat)))
      } else {
      ColLst <- split(x, J)  ## will do `factor(J)` internally in `split`
      }
    result <- duplicated.default(ColLst)
    } else {
    warning("invalid MARGIN; return NULL")
    result <- NULL
    }
  result
  }

which(duplicated.dgCMatrix(A, 2))
#[1] 7 8
duplicated.dgCMatrix运行良好,但存在纯零列的情况除外。例如:

库(矩阵)

i Nice解决方案和Nice使用
名称
,这些名称由
split
保存,例如,
i
duplicated.dgCMatrix <- function (dgCMat, MARGIN, include.all.zero.vectors = TRUE) {
  MARGIN <- as.integer(MARGIN)
  J <- rep(1:ncol(dgCMat), diff(dgCMat@p))
  I <- dgCMat@i + 1
  x <- dgCMat@x
  if (MARGIN == 1L) {
    ## check duplicated rows
    names(x) <- J
    if (include.all.zero.vectors) {
      RowLst <- split(x, factor(I, levels = 1:nrow(dgCMat)))
      } else {
      RowLst <- split(x, I)  ## will do `factor(I)` internally in `split`
      }
    result <- duplicated.default(RowLst)
    } else if (MARGIN == 2L) {
    ## check duplicated columns
    names(x) <- I
    if (include.all.zero.vectors) {
      ColLst <- split(x, factor(J, levels = 1:ncol(dgCMat)))
      } else {
      ColLst <- split(x, J)  ## will do `factor(J)` internally in `split`
      }
    result <- duplicated.default(ColLst)
    } else {
    warning("invalid MARGIN; return NULL")
    result <- NULL
    }
  result
  }

which(duplicated.dgCMatrix(A, 2))
#[1] 7 8
which(duplicated.dgCMatrix(A, 2))
#R [1] 2 5 6

# check that it works with the transpose
which(duplicated.dgCMatrix(t(A), 1))
#R [1] 2 5 6