Why does the function t return a t.test for objects with class set to "test"?

Viewed 193

I'm reading Hadley Wickham's book Advanced R, specifically the OO fied guide (http://adv-r.had.co.nz/OO-essentials.html). The first exercise in that chapter is as follows:

Read the source code for t() and t.test() and confirm that t.test() is an S3 generic and not an S3 method. What happens if you create an object with class test and call t() with it?

If I understood the chapter correctly, we can confirm that t() and t.test() are generic, because they use the UseMethod() function in the source code. methods(t) returns t.data.frame, t.default and t.ts* as the methods of function t(). Why then, if both are S3 generics and t does not have a t.test method, does the following code return the t test?

a <- structure(1:4, class = "test")
t(a)

My prediction would be that t would use the default method for class "test" and t.default(a) does the transpose as logically I suppose it should. Where then does the t.test come from?

1 Answers
Related