我有这样的循环,试图在这里实现解决方案,使用虚拟变量这样
aaa <- DFM %*% t(DFM) #DFM is Quanteda dfm-sparse-matrix
for(i in 1:nrow(aaa)) aaa[i,] <- aaa[i,][order(aaa[i,], decreasing = TRUE)]
但现在
for(i in 1:nrow(mmm)) mmm[i,] <- aaa[i,][order(aaa[i,], decreasing = TRUE)]
wheremmm
尚不存在,目标是做与mmm <- t(apply(a, 1, sort, decreasing = TRUE))
. 但现在在 for 循环之前我需要初始化mmm
else Error: object 'mmm' not found
。aaa
和mmm
的类型dgCMatrix
由两个 Quanteda DFM 矩阵的矩阵乘法给出。
结构
aaaFunc
由矩阵乘法给出,DFM %*% t(DFM)
其中 DFM 是 Quanteda Sparse dfm-matrix。结构是这样的
> str(aaaFunc) Formal class 'dgCMatrix' [package "Matrix"] with 6 slots ..@ i : int [1:39052309] 0 2 1 0 2 2616 2880 3 4 5 ... ..@ p : int [1:38162] 0 2 3 7 8 10 13 15 16 96 ... ..@ Dim : int [1:2] 38161 38161 ..@ Dimnames:List of 2 .. ..$ : chr [1:38161] "90120000" "90120000" "90120000" "86140000" ... .. ..$ : chr [1:38161] "90120000" "90120000" "90120000" "86140000" ... ..@ x : num [1:39052309] 1 1 1 1 2 1 1 1 2 1 ... ..@ factors : list()
使用此处提到的方法在 DFM 上出现错误,关于复制没有其内容但其结构/等的 R 对象的一般问题。
A.错误
aaaFunc.mt[]<- NA
> aaaFunc.mt <- aaaFunc[0,]; aaaFunc.mt[] <- NA; aaaFunc.mt[1,] Error in intI(i, n = x@Dim[1], dn[[1]], give.dn = FALSE) : index larger than maximal 0
B.错误
mySparseMatrix.mt[nrow(mySparseMatrix),]<-
> aaaFunc.mt <- aaaFunc[0,]; aaaFunc.mt[nrow(aaaFunc),] <- NA Error in intI(i, n = di[margin], dn = dn[[margin]], give.dn = FALSE) : index larger than maximal 0
C.错误
replace(...,NA)
Browse[2]> mmmFunc <- replace(aaaFunc,NA); Error in replace(aaaFunc, NA) : argument "values" is missing, with no default Browse[2]> mmmFunc <- replace(aaaFunc,,NA); Error in `[<-`(`*tmp*`, list, value = NA) : argument "list" is missing, with no default Browse[2]> mmmFunc <- replace(aaaFunc,c(),NA); Error in .local(x, i, j, ..., value) : not-yet-implemented 'Matrix[<-' method
您如何初始化由两个 Quanteda DFM 矩阵的矩阵乘法给出的空 dgCMatrix?