-
Notifications
You must be signed in to change notification settings - Fork 2
/
13-R-intro.Rmd
349 lines (342 loc) · 28.7 KB
/
13-R-intro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Writing your own functions
<p><a href="" id="index-Writing-functions"></a></p>
<p>As we have seen informally along the way, the R language allows the user to create objects of mode <em>function</em>. These are true R functions that are stored in a special internal form and may be used in further expressions and so on. In the process, the language gains enormously in power, convenience and elegance, and learning to write useful functions is one of the main ways to make your use of R comfortable and productive.</p>
<p>It should be emphasized that most of the functions supplied as part of the R system, such as <code class="calibre2">mean()</code>, <code class="calibre2">var()</code>, <code class="calibre2">postscript()</code> and so on, are themselves written in R and thus do not differ materially from user written functions.</p>
<p>A function is defined by an assignment of the form</p>
<div class="example">
<pre class="example1"><code>> name <- function(arg_1, arg_2, …) expression</code></pre>
</div>
<p><a href="" id="index-function"></a></p>
<p>The expression is an R expression, (usually a grouped expression), that uses the arguments, arg_i, to calculate a value. The value of the expression is the value returned for the function.</p>
<p>A call to the function then usually takes the form <code class="calibre2">name(expr_1, expr_2, …)</code> and may occur anywhere a function call is legitimate.</p>
<hr />
<p><a href="" id="Simple-examples"></a> <a href="" id="Simple-examples-1"></a></p>
<h3 id="simple-examples" class="section">10.1 Simple examples</h3>
<p>As a first example, consider a function to calculate the two sample <em>t</em>-statistic, showing “all the steps”. This is an artificial example, of course, since there are other, simpler ways of achieving the same end.</p>
<p>The function is defined as follows:</p>
<div class="example">
<pre class="example1"><code>> twosam <- function(y1, y2) {
n1 <- length(y1); n2 <- length(y2)
yb1 <- mean(y1); yb2 <- mean(y2)
s1 <- var(y1); s2 <- var(y2)
s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
tst <- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
tst
}</code></pre>
</div>
<p>With this function defined, you could perform two sample <em>t</em>-tests using a call such as</p>
<div class="example">
<pre class="example1"><code>> tstat <- twosam(data$male, data$female); tstat</code></pre>
</div>
<p>As a second example, consider a function to emulate directly the MATLAB backslash command, which returns the coefficients of the orthogonal projection of the vector <em>y</em> onto the column space of the matrix, <em>X</em>. (This is ordinarily called the least squares estimate of the regression coefficients.) This would ordinarily be done with the <code class="calibre2">qr()</code> function; however this is sometimes a bit tricky to use directly and it pays to have a simple function such as the following to use it safely.</p>
<p>Thus given a <em>n</em> by <em>1</em> vector <em>y</em> and an <em>n</em> by <em>p</em> matrix <em>X</em> then <em>X \ y</em> is defined as (X’X)^{-}X’y, where (X’X)^{-} is a generalized inverse of <em>X'X</em>.</p>
<div class="example">
<pre class="example1"><code>> bslash <- function(X, y) {
X <- qr(X)
qr.coef(X, y)
}</code></pre>
</div>
<p>After this object is created it may be used in statements such as</p>
<div class="example">
<pre class="example1"><code>> regcoeff <- bslash(Xmat, yvar)</code></pre>
</div>
<p>and so on.</p>
<p>The classical R function <code class="calibre2">lsfit()</code> does this job quite well, and more<a href="appendix-f-references.html#FOOT22" id="DOCF22"><sup>22</sup></a>. It in turn uses the functions <code class="calibre2">qr()</code> and <code class="calibre2">qr.coef()</code> in the slightly counterintuitive way above to do this part of the calculation. Hence there is probably some value in having just this part isolated in a simple to use function if it is going to be in frequent use. If so, we may wish to make it a matrix binary operator for even more convenient use.</p>
<hr />
<p><a href="" id="Defining-new-binary-operators"></a> <a href="" id="Defining-new-binary-operators-1"></a></p>
<h3 id="defining-new-binary-operators" class="section">10.2 Defining new binary operators</h3>
<p><a href="" id="index-Binary-operators"></a></p>
<p>Had we given the <code class="calibre2">bslash()</code> function a different name, namely one of the form</p>
<div class="example">
<pre class="example1"><code>%anything%</code></pre>
</div>
<p>it could have been used as a <em>binary operator</em> in expressions rather than in function form. Suppose, for example, we choose <code class="calibre2">!</code> for the internal character. The function definition would then start as</p>
<div class="example">
<pre class="example1"><code>> "%!%" <- function(X, y) { … }</code></pre>
</div>
<p>(Note the use of quote marks.) The function could then be used as <code class="calibre2">X %!% y</code>. (The backslash symbol itself is not a convenient choice as it presents special problems in this context.)</p>
<p>The matrix multiplication operator, <code class="calibre2">%*%</code>, and the outer product matrix operator <code class="calibre2">%o%</code> are other examples of binary operators defined in this way.</p>
<hr />
<p><a href="" id="Named-arguments-and-defaults"></a> <a href="" id="Named-arguments-and-defaults-1"></a></p>
<h3 id="named-arguments-and-defaults" class="section">10.3 Named arguments and defaults</h3>
<p><a href="" id="index-Named-arguments"></a> <a href="" id="index-Default-values"></a></p>
<p>As first noted in <a href="simple-manipulations-numbers-and-vectors.html#Generating-regular-sequences">Generating regular sequences</a>, if arguments to called functions are given in the “<code class="calibre2">name=object</code>” form, they may be given in any order. Furthermore the argument sequence may begin in the unnamed, positional form, and specify named arguments after the positional arguments.</p>
<p>Thus if there is a function <code class="calibre2">fun1</code> defined by</p>
<div class="example">
<pre class="example1"><code>> fun1 <- function(data, data.frame, graph, limit) {
[function body omitted]
}</code></pre>
</div>
<p>then the function may be invoked in several ways, for example</p>
<div class="example">
<pre class="example1"><code>> ans <- fun1(d, df, TRUE, 20)
> ans <- fun1(d, df, graph=TRUE, limit=20)
> ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df)</code></pre>
</div>
<p>are all equivalent.</p>
<p>In many cases arguments can be given commonly appropriate default values, in which case they may be omitted altogether from the call when the defaults are appropriate. For example, if <code class="calibre2">fun1</code> were defined as</p>
<div class="example">
<pre class="example1"><code>> fun1 <- function(data, data.frame, graph=TRUE, limit=20) { … }</code></pre>
</div>
<p>it could be called as</p>
<div class="example">
<pre class="example1"><code>> ans <- fun1(d, df)</code></pre>
</div>
<p>which is now equivalent to the three cases above, or as</p>
<div class="example">
<pre class="example1"><code>> ans <- fun1(d, df, limit=10)</code></pre>
</div>
<p>which changes one of the defaults.</p>
<p>It is important to note that defaults may be arbitrary expressions, even involving other arguments to the same function; they are not restricted to be constants as in our simple example here.</p>
<hr />
<p><a href="" id="The-three-dots-argument"></a> <a href="" id="The-_2026-argument"></a></p>
<h3 id="the-argument" class="section">10.4 The ‘…’ argument</h3>
<p>Another frequent requirement is to allow one function to pass on argument settings to another. For example many graphics functions use the function <code class="calibre2">par()</code> and functions like <code class="calibre2">plot()</code> allow the user to pass on graphical parameters to <code class="calibre2">par()</code> to control the graphical output. (See <a href="graphical-procedures.html#The-par_0028_0029-function">The par() function</a>, for more details on the <code class="calibre2">par()</code> function.) This can be done by including an extra argument, literally ‘…’, of the function, which may then be passed on. An outline example is given below.</p>
<div class="example">
<pre class="example1"><code>fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
[omitted statements]
if (graph)
par(pch="*", ...)
[more omissions]
}</code></pre>
</div>
<p>Less frequently, a function will need to refer to components of ‘…’. The expression <code class="calibre2">list(...)</code> evaluates all such arguments and returns them in a named list, while <code class="calibre2">..1</code>, <code class="calibre2">..2</code>, etc. evaluate them one at a time, with ‘..n’ returning the n’th unmatched argument.</p>
<hr />
<p><a href="" id="Assignment-within-functions"></a> <a href="" id="Assignments-within-functions"></a></p>
<h3 id="assignments-within-functions" class="section">10.5 Assignments within functions</h3>
<p>Note that <em>any ordinary assignments done within the function are local and temporary and are lost after exit from the function</em>. Thus the assignment <code class="calibre2">X <- qr(X)</code> does not affect the value of the argument in the calling program.</p>
<p>To understand completely the rules governing the scope of R assignments the reader needs to be familiar with the notion of an evaluation <em>frame</em>. This is a somewhat advanced, though hardly difficult, topic and is not covered further here.</p>
<p>If global and permanent assignments are intended within a function, then either the “superassignment” operator, <code class="calibre2"><<-</code> or the function <code class="calibre2">assign()</code> can be used. See the <code class="calibre2">help</code> document for details. S-PLUS users should be aware that <code class="calibre2"><<-</code> has different semantics in R. These are discussed further in <a href="#Scope">Scope</a>.</p>
<hr />
<p><a href="" id="More-advanced-examples"></a> <a href="" id="More-advanced-examples-1"></a></p>
<h3 id="more-advanced-examples" class="section">10.6 More advanced examples</h3>
<hr />
<p><a href="" id="Efficiency-factors-in-block-designs"></a> <a href="" id="Efficiency-factors-in-block-designs-1"></a></p>
<h4 id="efficiency-factors-in-block-designs" class="subheading">10.6.1 Efficiency factors in block designs</h4>
<p>As a more complete, if a little pedestrian, example of a function, consider finding the efficiency factors for a block design. (Some aspects of this problem have already been discussed in <a href="arrays-and-matrices.html#Index-matrices">Index matrices</a>.)</p>
<p>A block design is defined by two factors, say <code class="calibre2">blocks</code> (<code class="calibre2">b</code> levels) and <code class="calibre2">varieties</code> (<code class="calibre2">v</code> levels). If <em>R</em> and <em>K</em> are the <em>v</em> by <em>v</em> and <em>b</em> by <em>b</em> <em>replications</em> and <em>block size</em> matrices, respectively, and <em>N</em> is the <em>b</em> by <em>v</em> incidence matrix, then the efficiency factors are defined as the eigenvalues of the matrix E = I_v - R^{-1/2}N’K^{-1}NR^{-1/2} = I_v - A’A, where A = K^{-1/2}NR^{-1/2}. One way to write the function is given below.</p>
<div class="example">
<pre class="example1"><code>> bdeff <- function(blocks, varieties) {
blocks <- as.factor(blocks) # minor safety move
b <- length(levels(blocks))
varieties <- as.factor(varieties) # minor safety move
v <- length(levels(varieties))
K <- as.vector(table(blocks)) # remove dim attr
R <- as.vector(table(varieties)) # remove dim attr
N <- table(blocks, varieties)
A <- 1/sqrt(K) * N * rep(1/sqrt(R), rep(b, v))
sv <- svd(A)
list(eff=1 - sv$d^2, blockcv=sv$u, varietycv=sv$v)
}</code></pre>
</div>
<p>It is numerically slightly better to work with the singular value decomposition on this occasion rather than the eigenvalue routines.</p>
<p>The result of the function is a list giving not only the efficiency factors as the first component, but also the block and variety canonical contrasts, since sometimes these give additional useful qualitative information.</p>
<hr />
<p><a href="" id="Dropping-all-names-in-a-printed-array"></a> <a href="" id="Dropping-all-names-in-a-printed-array-1"></a></p>
<h4 id="dropping-all-names-in-a-printed-array" class="subheading">10.6.2 Dropping all names in a printed array</h4>
<p>For printing purposes with large matrices or arrays, it is often useful to print them in close block form without the array names or numbers. Removing the <code class="calibre2">dimnames</code> attribute will not achieve this effect, but rather the array must be given a <code class="calibre2">dimnames</code> attribute consisting of empty strings. For example to print a matrix, <code class="calibre2">X</code></p>
<div class="example">
<pre class="example1"><code>> temp <- X
> dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)))
> temp; rm(temp)</code></pre>
</div>
<p>This can be much more conveniently done using a function, <code class="calibre2">no.dimnames()</code>, shown below, as a “wrap around” to achieve the same result. It also illustrates how some effective and useful user functions can be quite short.</p>
<div class="example">
<pre class="example1"><code>no.dimnames <- function(a) {
## Remove all dimension names from an array for compact printing.
d <- list()
l <- 0
for(i in dim(a)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(a) <- d
a
}</code></pre>
</div>
<p>With this function defined, an array may be printed in close format using</p>
<div class="example">
<pre class="example1"><code>> no.dimnames(X)</code></pre>
</div>
<p>This is particularly useful for large integer arrays, where patterns are the real interest rather than the values.</p>
<hr />
<p><a href="" id="Recursive-numerical-integration"></a> <a href="" id="Recursive-numerical-integration-1"></a></p>
<h4 id="recursive-numerical-integration" class="subheading">10.6.3 Recursive numerical integration</h4>
<p>Functions may be recursive, and may themselves define functions within themselves. Note, however, that such functions, or indeed variables, are not inherited by called functions in higher evaluation frames as they would be if they were on the search path.</p>
<p>The example below shows a naive way of performing one-dimensional numerical integration. The integrand is evaluated at the end points of the range and in the middle. If the one-panel trapezium rule answer is close enough to the two panel, then the latter is returned as the value. Otherwise the same process is recursively applied to each panel. The result is an adaptive integration process that concentrates function evaluations in regions where the integrand is farthest from linear. There is, however, a heavy overhead, and the function is only competitive with other algorithms when the integrand is both smooth and very difficult to evaluate.</p>
<p>The example is also given partly as a little puzzle in R programming.</p>
<div class="example">
<pre class="example1"><code>area <- function(f, a, b, eps = 1.0e-06, lim = 10) {
fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) {
## function ‘fun1’ is only visible inside ‘area’
d <- (a + b)/2
h <- (b - a)/4
fd <- f(d)
a1 <- h * (fa + fd)
a2 <- h * (fd + fb)
if(abs(a0 - a1 - a2) < eps || lim == 0)
return(a1 + a2)
else {
return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun) +
fun(f, d, b, fd, fb, a2, eps, lim - 1, fun))
}
}
fa <- f(a)
fb <- f(b)
a0 <- ((fa + fb) * (b - a))/2
fun1(f, a, b, fa, fb, a0, eps, lim, fun1)
}</code></pre>
</div>
<hr />
<p><a href="" id="Scope"></a> <a href="" id="Scope-1"></a></p>
<h3 id="scope" class="section">10.7 Scope</h3>
<p><a href="" id="index-Scope"></a></p>
<p>The discussion in this section is somewhat more technical than in other parts of this document. However, it details one of the major differences between S-PLUS and R.</p>
<p>The symbols which occur in the body of a function can be divided into three classes; formal parameters, local variables and free variables. The formal parameters of a function are those occurring in the argument list of the function. Their values are determined by the process of <em>binding</em> the actual function arguments to the formal parameters. Local variables are those whose values are determined by the evaluation of expressions in the body of the functions. Variables which are not formal parameters or local variables are called free variables. Free variables become local variables if they are assigned to. Consider the following function definition.</p>
<div class="example">
<pre class="example1"><code>f <- function(x) {
y <- 2*x
print(x)
print(y)
print(z)
}</code></pre>
</div>
<p>In this function, <code class="calibre2">x</code> is a formal parameter, <code class="calibre2">y</code> is a local variable and <code class="calibre2">z</code> is a free variable.</p>
<p>In R the free variable bindings are resolved by first looking in the environment in which the function was created. This is called <em>lexical scope</em>. First we define a function called <code class="calibre2">cube</code>.</p>
<div class="example">
<pre class="example1"><code>cube <- function(n) {
sq <- function() n*n
n*sq()
}</code></pre>
</div>
<p>The variable <code class="calibre2">n</code> in the function <code class="calibre2">sq</code> is not an argument to that function. Therefore it is a free variable and the scoping rules must be used to ascertain the value that is to be associated with it. Under static scope (S-PLUS) the value is that associated with a global variable named <code class="calibre2">n</code>. Under lexical scope (R) it is the parameter to the function <code class="calibre2">cube</code> since that is the active binding for the variable <code class="calibre2">n</code> at the time the function <code class="calibre2">sq</code> was defined. The difference between evaluation in R and evaluation in S-PLUS is that S-PLUS looks for a global variable called <code class="calibre2">n</code> while R first looks for a variable called <code class="calibre2">n</code> in the environment created when <code class="calibre2">cube</code> was invoked.</p>
<div class="example">
<pre class="example1"><code>## first evaluation in S
S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
## then the same function evaluated in R
R> cube(2)
[1] 8</code></pre>
</div>
<p>Lexical scope can also be used to give functions <em>mutable state</em>. In the following example we show how R can be used to mimic a bank account. A functioning bank account needs to have a balance or total, a function for making withdrawals, a function for making deposits and a function for stating the current balance. We achieve this by creating the three functions within <code class="calibre2">account</code> and then returning a list containing them. When <code class="calibre2">account</code> is invoked it takes a numerical argument <code class="calibre2">total</code> and returns a list containing the three functions. Because these functions are defined in an environment which contains <code class="calibre2">total</code>, they will have access to its value.</p>
<p>The special assignment operator, <code class="calibre2"><<-</code>, <a href="" id="index-_003c_003c_002d"></a> is used to change the value associated with <code class="calibre2">total</code>. This operator looks back in enclosing environments for an environment that contains the symbol <code class="calibre2">total</code> and when it finds such an environment it replaces the value, in that environment, with the value of right hand side. If the global or top-level environment is reached without finding the symbol <code class="calibre2">total</code> then that variable is created and assigned to there. For most users <code class="calibre2"><<-</code> creates a global variable and assigns the value of the right hand side to it<a href="appendix-f-references.html#FOOT23" id="DOCF23"><sup>23</sup></a>. Only when <code class="calibre2"><<-</code> has been used in a function that was returned as the value of another function will the special behavior described here occur.</p>
<div class="example">
<pre class="example1"><code>open.account <- function(total) {
list(
deposit = function(amount) {
if(amount <= 0)
stop("Deposits must be positive!\n")
total <<- total + amount
cat(amount, "deposited. Your balance is", total, "\n\n")
},
withdraw = function(amount) {
if(amount > total)
stop("You don't have that much money!\n")
total <<- total - amount
cat(amount, "withdrawn. Your balance is", total, "\n\n")
},
balance = function() {
cat("Your balance is", total, "\n\n")
}
)
}
ross <- open.account(100)
robert <- open.account(200)
ross$withdraw(30)
ross$balance()
robert$balance()
ross$deposit(50)
ross$balance()
ross$withdraw(500)</code></pre>
</div>
<hr />
<p><a href="" id="Customizing-the-environment"></a> <a href="" id="Customizing-the-environment-1"></a></p>
<h3 id="customizing-the-environment" class="section">10.8 Customizing the environment</h3>
<p><a href="" id="index-Customizing-the-environment"></a></p>
<p>Users can customize their environment in several different ways. There is a site initialization file and every directory can have its own special initialization file. Finally, the special functions <code class="calibre2">.First</code> and <code class="calibre2">.Last</code> can be used.</p>
<p>The location of the site initialization file is taken from the value of the <code class="calibre2">R_PROFILE</code> environment variable. If that variable is unset, the file Rprofile.site in the R home subdirectory etc is used. This file should contain the commands that you want to execute every time R is started under your system. A second, personal, profile file named .Rprofile<a href="appendix-f-references.html#FOOT24" id="DOCF24"><sup>24</sup></a> can be placed in any directory. If R is invoked in that directory then that file will be sourced. This file gives individual users control over their workspace and allows for different startup procedures in different working directories. If no .Rprofile file is found in the startup directory, then R looks for a .Rprofile file in the user’s home directory and uses that (if it exists). If the environment variable <code class="calibre2">R_PROFILE_USER</code> is set, the file it points to is used instead of the .Rprofile files.</p>
<p>Any function named <code class="calibre2">.First()</code> in either of the two profile files or in the .RData image has a special status. It is automatically performed at the beginning of an R session and may be used to initialize the environment. For example, the definition in the example below alters the prompt to <code class="calibre2">$</code> and sets up various other useful things that can then be taken for granted in the rest of the session.</p>
<p>Thus, the sequence in which files are executed is, Rprofile.site, the user profile, .RData and then <code class="calibre2">.First()</code>. A definition in later files will mask definitions in earlier files.</p>
<div class="example">
<pre class="example1"><code>> .First <- function() {
options(prompt="$ ", continue="+\t") # $ is the prompt
options(digits=5, length=999) # custom numbers and printout
x11() # for graphics
par(pch = "+") # plotting character
source(file.path(Sys.getenv("HOME"), "R", "mystuff.R"))
# my personal functions
library(MASS) # attach a package
}</code></pre>
</div>
<p><a href="" id="index-_002eFirst"></a></p>
<p>Similarly a function <code class="calibre2">.Last()</code>, if defined, is (normally) executed at the very end of the session. An example is given below.</p>
<div class="example">
<pre class="example1"><code>> .Last <- function() {
graphics.off() # a small safety measure.
cat(paste(date(),"\nAdios\n")) # Is it time for lunch?
}</code></pre>
</div>
<p><a href="" id="index-_002eLast"></a></p>
<hr />
<p><a href="" id="Object-orientation"></a> <a href="" id="Classes_002c-generic-functions-and-object-orientation"></a></p>
<h3 id="classes-generic-functions-and-object-orientation" class="section">10.9 Classes, generic functions and object orientation</h3>
<p><a href="" id="index-Classes-1"></a> <a href="" id="index-Generic-functions"></a> <a href="" id="index-Object-orientation"></a></p>
<p>The class of an object determines how it will be treated by what are known as <em>generic</em> functions. Put the other way round, a generic function performs a task or action on its arguments <em>specific to the class of the argument itself</em>. If the argument lacks any <code class="calibre2">class</code> attribute, or has a class not catered for specifically by the generic function in question, there is always a <em>default action</em> provided.</p>
<p>An example makes things clearer. The class mechanism offers the user the facility of designing and writing generic functions for special purposes. Among the other generic functions are <code class="calibre2">plot()</code> for displaying objects graphically, <code class="calibre2">summary()</code> for summarizing analyses of various types, and <code class="calibre2">anova()</code> for comparing statistical models.</p>
<p>The number of generic functions that can treat a class in a specific way can be quite large. For example, the functions that can accommodate in some fashion objects of class <code class="calibre2">"data.frame"</code> include</p>
<div class="example">
<pre class="example1"><code>[ [[<- any as.matrix
[<- mean plot summary</code></pre>
</div>
<p><a href="" id="index-methods"></a></p>
<p>A currently complete list can be got by using the <code class="calibre2">methods()</code> function:</p>
<div class="example">
<pre class="example1"><code>> methods(class="data.frame")</code></pre>
</div>
<p>Conversely the number of classes a generic function can handle can also be quite large. For example the <code class="calibre2">plot()</code> function has a default method and variants for objects of classes <code class="calibre2">"data.frame"</code>, <code class="calibre2">"density"</code>, <code class="calibre2">"factor"</code>, and more. A complete list can be got again by using the <code class="calibre2">methods()</code> function:</p>
<div class="example">
<pre class="example1"><code>> methods(plot)</code></pre>
</div>
<p>For many generic functions the function body is quite short, for example</p>
<div class="example">
<pre class="example1"><code>> coef
function (object, ...)
UseMethod("coef")</code></pre>
</div>
<p>The presence of <code class="calibre2">UseMethod</code> indicates this is a generic function. To see what methods are available we can use <code class="calibre2">methods()</code></p>
<div class="example">
<pre class="example1"><code>> methods(coef)
[1] coef.aov* coef.Arima* coef.default* coef.listof*
[5] coef.nls* coef.summary.nls*
Non-visible functions are asterisked</code></pre>
</div>
<p>In this example there are six methods, none of which can be seen by typing its name. We can read these by either of</p>
<p><a href="" id="index-getAnywhere"></a> <a href="" id="index-getS3method"></a></p>
<div class="example">
<pre class="example1"><code>> getAnywhere("coef.aov")
A single object matching ‘coef.aov’ was found
It was found in the following places
registered S3 method for coef from namespace stats
namespace:stats
with value
function (object, ...)
{
z <- object$coef
z[!is.na(z)]
}
> getS3method("coef", "aov")
function (object, ...)
{
z <- object$coef
z[!is.na(z)]
}</code></pre>
</div>
<p>A function named <code class="calibre2">gen.cl</code> will be invoked by the generic <code class="calibre2">gen</code> for class <code class="calibre2">cl</code>, so do not name functions in this style unless they are intended to be methods.</p>
<p>The reader is referred to the <em>R Language Definition</em> for a more complete discussion of this mechanism.</p>
<hr />
<p><a href="" id="Statistical-models-in-R"></a> <a href="" id="Statistical-models-in-R-1"></a></p>
<div id="calibre_pb_24" class="calibre8">
</div>