有兩個單獨的問題,其實。第一個是如何顯示零。這是很容易通過尋找被調度之後所使用的確切的方法來解決:
Matrix::printSpMatrix(toy, zero.print="0")
[1,] 0 0 0
[2,] 1 1 1
[3,] NA NA NA
第二個問題是,是否NA
輸出可以與一些其它字符被抑制。那麼,這是不可能的:沒有適合的參數。
然而,修改源始終是一個選項。注意:這是一種黑客攻擊,可能會導致不可預料的後果!
toy_print <- function (x, digits = NULL, maxp = getOption("max.print"), cld = getClassDef(class(x)),
zero.print = ".", col.names, note.dropping.colnames = TRUE,
col.trailer = "", align = c("fancy", "right"))
{
stopifnot(extends(cld, "sparseMatrix"))
x.orig <- x
cx <- formatSpMatrix(x, digits = digits, maxp = maxp, cld = cld,
zero.print = zero.print, col.names = col.names, note.dropping.colnames = note.dropping.colnames,
align = align)
if (col.trailer != "")
cx <- cbind(cx, col.trailer, deparse.level = 0)
# here's the NA hack
cx[cx=="NA"] <- "."
print(cx, quote = FALSE, right = TRUE, max = maxp)
invisible(x.orig)
}
toy_print(toy, zero.print="0")
[1,] 0 0 0
[2,] 1 1 1
[3,] . . .
感謝您的幫助。我跑這個代碼,我收到了不同的輸出給你:[1,] 0 0 0 [2,] 1 1 1 [3,] 1 1 1 – joel38237
我剛剛仔細檢查了代碼,似乎很好。 – tonytonov
我已經清理了我的工作區,現在我得到和你一樣的答案。再次感謝您的幫助和有趣的解決方案。 – joel38237