【发布时间】:2019-10-13 11:41:25
【问题描述】:
R 中的统计测试会生成列表,但是当您调用测试时,这些列表的打印会提供一个特殊的用户友好结构来帮助读者。要了解我在说什么,请考虑一个使用 stats 包中的 t.test 函数的示例。
#Run a T-test on some example data
X <- c(30, 32, 40, 28, 29, 35, 30, 34, 31, 39);
Y <- c(19, 20, 44, 45, 8, 29, 26, 59, 35, 50);
TEST <- stats::t.test(X,Y);
#Show structure of the TEST object
str(TEST);
List of 9
$ statistic : Named num -0.134
..- attr(*, "names")= chr "t"
$ parameter : Named num 10.2
..- attr(*, "names")= chr "df"
$ p.value : num 0.896
$ conf.int : num [1:2] -12.3 10.9
..- attr(*, "conf.level")= num 0.95
$ estimate : Named num [1:2] 32.8 33.5
..- attr(*, "names")= chr [1:2] "mean of x" "mean of y"
$ null.value : Named num 0
..- attr(*, "names")= chr "difference in means"
$ alternative: chr "two.sided"
$ method : chr "Welch Two Sample t-test"
$ data.name : chr "X and Y"
- attr(*, "class")= chr "htest"
这个对象是一个包含九个元素的列表,其中一些是通过属性命名的。但是,当我打印TEST 对象时,返回的信息的结构与标准的列表打印方式不同。
#Print the TEST object
TEST;
Welch Two Sample t-test
data: X and Y
t = -0.13444, df = 10.204, p-value = 0.8957
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-12.27046 10.87046
sample estimates:
mean of x mean of y
32.8 33.5
如您所见,此打印输出比列表的标准打印更加用户友好。我希望能够在R 中编写统计测试,生成与上述类似的输出列表,但以这种用户友好的方式打印。
我的问题: 为什么R 以这种特殊方式打印列表TEST 的输出?如果我创建一个统计测试的输出列表(例如,如上所述),我该如何设置对象以这种方式打印?
【问题讨论】:
-
查看结果的类,然后查看'methods(print)'。
标签: r pretty-print