How to Predict New Data Values with R
How to Calculate Data Proportions and Find the Center in R
How to Reduce the Number of Lines in R

How to Dispatch to a Method in R

A useful tool when dealing with functions in R is dispatching to a method. One thing to remember about functions in R is that one function can give a different result based on the type of value you give the arguments R has a genius system, called the generic function system, that allows you to call different functions using the same name.

Think about data frames and lists. If you print a list in the console, you get the output arranged in rows. On the other hand, a data frame is printed to the console arranged in columns. So, the print() function treats lists and data frames differently, but both times you used the same function.

Find the methods behind the function

It’s easy to find out if you used the same function both times — just peek inside the function code of print() by typing its name at the command line, like this:

> print
function (x, ...)
UseMethod("print")
<bytecode: 0x0464f9e4>
<environment: namespace:base>

You can safely ignore the two last lines because they are used only by R developers. But take a look at the function body — it’s only one line!

Functions that don’t do much other than passing on objects to the right function are called generic functions. In this example, print() is a generic function. The functions that do the actual work are called methods.

Use methods with UseMethod

How on earth can that one line of code in the print() function do so many complex things like printing vectors, data frames, and lists all in a different way?

The answer is contained in the UseMethod() function, which is the central function in the generic function system of R. UseMethod() tells R to move along and look for a function that can deal with the type of object that is given as the argument x.

R does that by looking through the complete set of functions in search of another function that starts with print followed by a dot and then the name of the object type.

You can do that yourself by using the command apropos('print\\.'). Between the quotation marks, you can put a regular expression much like in the grep() function. In order to tell R that the dot really means a dot, you have to precede it with two backslashes. Don’t be surprised when you get over 40 different print() functions for all kinds of objects.

Suppose you have a data frame you want to print. R will look up the function print.data.frame() and use that function to print the object you passed as an argument. You also can call that function yourself like this:

> small.one <- data.frame(a = 1:2, b = 2:1)
> print.data.frame(small.one)
 a b
1 1 2
2 2 1

The effect of that function differs in no way from what you would get if you used the generic print(small.one) function instead. That’s because print() will give the small.one to the print.data.frame() function to take care of it.

Use default methods

In the case of a list, you may be tempted to look for a print.list() function. But it won’t work, because the print.list() function doesn’t exist. Still that isn’t a problem for R — R will ignore the type of the object in that case and just look for a default method, print.default().

For many generic functions, there is a default method that’s used if no specific method can be found. If there is one, you can recognize the default method by the word default after the dot in the function name.

So, if you want to print the data frame as a list, use the default method like this:

> print.default(small.one)
$a
[1] 1 2
$b
[1] 2 1
attr(,"class")
[1] "data.frame"
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Install R
How to Construct Vectors in R
How to Analyze Data Variances in Models with R
How to Test Data Normality Graphically in R
How R Calculates Infinite, Undefined, and Missing Values
Advertisement

Inside Dummies.com