The Art of R Programming

(WallPaper) #1
function, a lot of the function would zoom by on the screen too fast for us to
absorb it. We could usepage()to solve this problem, but I preferedit():

> edit(table)

This allows you to browse through the code with your text editor. In
doing so, you’ll find this code at the end:

y <- array(tabulate(bin, pd), dims, dimnames = dn)
class(y) <- "table"
y

Ah, interesting. This shows thattable()is, to some extent, a wrapper for
another function,tabulate(). But what might be more important here is that
the structure of a"table"object is really pretty simple: It consists of an array
created from the counts, with the class attribute tacked on. So, it’s essentially
just an array.
The functionnames()shows the components in an object, andattributes()
gives you this and a bit more, notably the class name.

9.4.5 The exists() Function.............................................


The functionexists()returnsTRUEorFALSE, depending on whether the argu-
ment exists. Be sure to put the argument in quotation marks.
For example, the following code shows that theaccobject exists:

> exists("acc")
[1] TRUE

Why would this function be useful? Don’t we always know whether or
not we’ve created an object and whether it’s still there? Not necessarily. If
you are writing general-purpose code, say to be made available to the world
in R’s CRAN code repository, your code may need to check whether a cer-
tain object exists, and if it doesn’t, then your code must create it. For exam-
ple, as you learned in Section 9.4.3, you can save objects to disk files using
save()and then later restore them to R’s memory space by callingload().
You might write general-purpose code that makes the latter call if the object
is not already present, a condition you could check by callingexists().

230 Chapter 9

Free download pdf