要看看发生了什么我总是建议保存感兴趣的对象,然后使用str
来查看其结构。
library(ltm)
library(xtable)
mat <- matrix(rnorm(1000), 100, 10, dimnames = list(NULL, LETTERS[1:10]))
out <- rcor.test(mat)
str(out)
它看起来好像正在打印的表格实际上并没有存储在这里。所以,让我们来看看在打印方法rcor.test
getAnywhere(print.rcor.test)
我们可以看到,该方法实际上构造了被打印出来,但不会返回它的基质。所以要得到矩阵,以便我们可以使用xtable,我们只需要...窃取代码来构造矩阵。不是打印矩阵然后返回原始对象,而是返回构造的矩阵。
get.rcor.test.matrix <- function (x, digits = max(3, getOption("digits") - 4), ...)
{
### Modified from print.rcor.test
mat <- x$cor.mat
mat[lower.tri(mat)] <- x$p.values[, 3]
mat[upper.tri(mat)] <- sprintf("%6.3f", as.numeric(mat[upper.tri(mat)]))
mat[lower.tri(mat)] <- sprintf("%6.3f", as.numeric(mat[lower.tri(mat)]))
ind <- mat[lower.tri(mat)] == paste(" 0.", paste(rep(0, digits),
collapse = ""), sep = "")
mat[lower.tri(mat)][ind] <- "<0.001"
ind <- mat[lower.tri(mat)] == paste(" 1.", paste(rep(0, digits),
collapse = ""), sep = "")
mat[lower.tri(mat)][ind] <- ">0.999"
diag(mat) <- " *****"
cat("\n")
## Now for the modifications
return(mat)
## and ignore the rest
#print(noquote(mat))
#cat("\nupper diagonal part contains correlation coefficient estimates",
# "\nlower diagonal part contains corresponding p-values\n\n")
#invisible(x)
}
现在让我们来得到我们的矩阵,并使用它的xtable。你
ourmatrix <- get.rcor.test.matrix(out)
xtable(ourmatrix)
在一个侧面说明 - 我建议检查出knitr。它基本上是Sweave,但很多*使用更好。 – Dason
感谢@Dason为您的好建议。 – MYaseen208