有两个单独的问题,其实。第一个是如何显示零。这是很容易通过寻找被调度之后所使用的确切的方法来解决:
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