Exercise

What's in a Name?

S3 uses a strict naming convention: all S3 methods have a name of the form generic.class.

The converse is not true: a function can have a name containing a dot without being an S3 method. This is the case with many of the functions that have been around since the early days of the S language. For example, all.equal() is actually an S3 generic, not a method. (This is an example of how leopard.case can be confusing.)

You can check if a function is an S3 generic by calling is_s3_generic() from the pryr package. You can also print it (by typing its name in the console), then looking to see if it calls UseMethod().

Similarly, you can check if a function is an S3 method by calling is_s3_method() from pryr. For example,

library(pryr)
is_s3_generic("t")           # generic transpose function
is_s3_method("t.data.frame") # transpose method for data.frames
is_s3_method("t.test")       # a function for Student's t-tests 

Which statements are true?

  1. is.complex() is a method of the is generic that acts on complex objects.
  2. seq.Date() is a method of the seq generic that acts on Date objects.
  3. is.na.data.frame() is a method of the is.na generic that acts on data.frame objects.
  4. sort() is a generic function.
  5. order() is a generic function.

Instructions

50 XP

Possible answers