Statistical tests in R
generate lists, but then when you call the test, the printing of these lists gives a special user-friendly structure to assist the reader. To see what I'm talking about, consider an example where you use the t.test
function in the stats
package.
#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"
This object is a list with nine elements, some of which are named via attributes. However, when I print the TEST
object, the returned information is structured in a different way than the standard printing of a list.
#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
As you can see, this printed output is much more user-friendly than the standard printing for a list. I would like to be able to program statistical tests in R
which generate a list of outputs similar to the above, but which print in this user-friendly way.
My Questions: Why does R
print the output of the list TEST
in this special way? If I create a list of outputs of a statistical test (e.g., like the above), how can I set the object to print in this way?