为什么表格输出出现在上面而不是下面连着的句子?

时间:2014-11-11 09:47:42

标签: r matrix

我有代码

cat("Misclassification table of predicted y versus true y:","\n",
  print(table(pred = pred.class, true = test$low))

它提供了输出

true
pred  0  1
   0 10  6
   1  1  2
Misclassification table of predicted y versus true y: 
 10 1 6 2

我不确定为什么表格出现在连接句子的上方?

输出应该看起来像

Misclassification table of predicted y versus true y: 
true
pred  0  1
   0 10  6
   1  1  2

1 个答案:

答案 0 :(得分:0)

这是你经常需要做的吗?如果是,我建议你创建一个自定义打印方法,如下例所示:

set.seed(1)
x <- table(true = sample(0:1, 20, TRUE),
           pred = sample(0:1, 20, TRUE))
class(x) <- c("myClass", class(x))
print.myClass <- function(x, ...) {
   cat("Misclassification table of predicted y versus true y:","\n\n")
   print.table(x)
}
x
# Misclassification table of predicted y versus true y: 
# 
#     pred
# true 0 1
#    0 5 4
#    1 7 4

否则,您可以探索capture.output

y <- unclass(x)
y
#     pred
# true 0 1
#    0 5 4
#    1 7 4

cat("blah blah blah\n", capture.output(y), sep = "\n")
# blah blah blah
# 
#     pred
# true 0 1
#    0 5 4
#    1 7 4