-
Notifications
You must be signed in to change notification settings - Fork 2
/
06-R-intro.Rmd
79 lines (78 loc) · 10.2 KB
/
06-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
# Objects, their modes and attributes
<p><a href="" id="index-Objects"></a> <a href="" id="index-Attributes"></a></p>
<hr />
<p><a href="" id="The-intrinsic-attributes-mode-and-length"></a> <a href="" id="Intrinsic-attributes_003a-mode-and-length"></a></p>
<h3 id="intrinsic-attributes-mode-and-length" class="section">3.1 Intrinsic attributes: mode and length</h3>
<p>The entities R operates on are technically known as <em>objects</em>. Examples are vectors of numeric (real) or complex values, vectors of logical values and vectors of character strings. These are known as “atomic” structures since their components are all of the same type, or <em>mode</em>, namely <em>numeric</em><a href="appendix-f-references.html#FOOT10" id="DOCF10"><sup>10</sup></a>, <em>complex</em>, <em>logical</em>, <em>character</em> and <em>raw</em>.</p>
<p>Vectors must have their values <em>all of the same mode</em>. Thus any given vector must be unambiguously either <em>logical</em>, <em>numeric</em>, <em>complex</em>, <em>character</em> or <em>raw</em>. (The only apparent exception to this rule is the special “value” listed as <code class="calibre2">NA</code> for quantities not available, but in fact there are several types of <code class="calibre2">NA</code>). Note that a vector can be empty and still have a mode. For example the empty character string vector is listed as <code class="calibre2">character(0)</code> and the empty numeric vector as <code class="calibre2">numeric(0)</code>.</p>
<p>R also operates on objects called <em>lists</em>, which are of mode <em>list</em>. These are ordered sequences of objects which individually can be of any mode. <em>lists</em> are known as “recursive” rather than atomic structures since their components can themselves be lists in their own right.</p>
<p>The other recursive structures are those of mode <em>function</em> and <em>expression</em>. Functions are the objects that form part of the R system along with similar user written functions, which we discuss in some detail later. Expressions as objects form an advanced part of R which will not be discussed in this guide, except indirectly when we discuss <em>formulae</em> used with modeling in R.</p>
<p>By the <em>mode</em> of an object we mean the basic type of its fundamental constituents. This is a special case of a “property” of an object. Another property of every object is its <em>length</em>. The functions <code class="calibre2">mode(object)</code> and <code class="calibre2">length(object)</code> can be used to find out the mode and length of any defined structure <a href="appendix-f-references.html#FOOT11" id="DOCF11"><sup>11</sup></a>.</p>
<p>Further properties of an object are usually provided by <code class="calibre2">attributes(object)</code>, see <a href="#Getting-and-setting-attributes">Getting and setting attributes</a>. Because of this, <em>mode</em> and <em>length</em> are also called “intrinsic attributes” of an object. <a href="" id="index-mode"></a> <a href="" id="index-length-1"></a></p>
<p>For example, if <code class="calibre2">z</code> is a complex vector of length 100, then in an expression <code class="calibre2">mode(z)</code> is the character string <code class="calibre2">"complex"</code> and <code class="calibre2">length(z)</code> is <code class="calibre2">100</code>.</p>
<p>R caters for changes of mode almost anywhere it could be considered sensible to do so, (and a few where it might not be). For example with</p>
<div class="example">
<pre class="example1"><code>> z <- 0:9</code></pre>
</div>
<p>we could put</p>
<div class="example">
<pre class="example1"><code>> digits <- as.character(z)</code></pre>
</div>
<p>after which <code class="calibre2">digits</code> is the character vector <code class="calibre2">c("0", "1", "2", …, "9")</code>. A further <em>coercion</em>, or change of mode, reconstructs the numerical vector again:</p>
<div class="example">
<pre class="example1"><code>> d <- as.integer(digits)</code></pre>
</div>
<p>Now <code class="calibre2">d</code> and <code class="calibre2">z</code> are the same.<a href="appendix-f-references.html#FOOT12" id="DOCF12"><sup>12</sup></a> There is a large collection of functions of the form <code class="calibre2">as.something()</code> for either coercion from one mode to another, or for investing an object with some other attribute it may not already possess. The reader should consult the different help files to become familiar with them.</p>
<hr />
<p><a href="" id="Changing-the-length-of-an-object"></a> <a href="" id="Changing-the-length-of-an-object-1"></a></p>
<h3 id="changing-the-length-of-an-object" class="section">3.2 Changing the length of an object</h3>
<p>An “empty” object may still have a mode. For example</p>
<div class="example">
<pre class="example1"><code>> e <- numeric()</code></pre>
</div>
<p>makes <code class="calibre2">e</code> an empty vector structure of mode numeric. Similarly <code class="calibre2">character()</code> is a empty character vector, and so on. Once an object of any size has been created, new components may be added to it simply by giving it an index value outside its previous range. Thus</p>
<div class="example">
<pre class="example1"><code>> e[3] <- 17</code></pre>
</div>
<p>now makes <code class="calibre2">e</code> a vector of length 3, (the first two components of which are at this point both <code class="calibre2">NA</code>). This applies to any structure at all, provided the mode of the additional component(s) agrees with the mode of the object in the first place.</p>
<p>This automatic adjustment of lengths of an object is used often, for example in the <code class="calibre2">scan()</code> function for input. (see <a href="reading-data-from-files.html#The-scan_0028_0029-function">The scan() function</a>.)</p>
<p>Conversely to truncate the size of an object requires only an assignment to do so. Hence if <code class="calibre2">alpha</code> is an object of length 10, then</p>
<div class="example">
<pre class="example1"><code>> alpha <- alpha[2 * 1:5]</code></pre>
</div>
<p>makes it an object of length 5 consisting of just the former components with even index. (The old indices are not retained, of course.) We can then retain just the first three values by</p>
<div class="example">
<pre class="example1"><code>> length(alpha) <- 3</code></pre>
</div>
<p>and vectors can be extended (by missing values) in the same way.</p>
<hr />
<p><a href="" id="Getting-and-setting-attributes"></a> <a href="" id="Getting-and-setting-attributes-1"></a></p>
<h3 id="getting-and-setting-attributes" class="section">3.3 Getting and setting attributes</h3>
<p><a href="" id="index-attr"></a> <a href="" id="index-attributes"></a></p>
<p>The function <code class="calibre2">attributes(object)</code> <a href="" id="index-attributes-1"></a> returns a list of all the non-intrinsic attributes currently defined for that object. The function <code class="calibre2">attr(object, name)</code> <a href="" id="index-attr-1"></a> can be used to select a specific attribute. These functions are rarely used, except in rather special circumstances when some new attribute is being created for some particular purpose, for example to associate a creation date or an operator with an R object. The concept, however, is very important.</p>
<p>Some care should be exercised when assigning or deleting attributes since they are an integral part of the object system used in R.</p>
<p>When it is used on the left hand side of an assignment it can be used either to associate a new attribute with <code class="calibre2">object</code> or to change an existing one. For example</p>
<div class="example">
<pre class="example1"><code>> attr(z, "dim") <- c(10,10)</code></pre>
</div>
<p>allows R to treat <code class="calibre2">z</code> as if it were a 10-by-10 matrix.</p>
<hr />
<p><a href="" id="The-class-of-an-object"></a> <a href="" id="The-class-of-an-object-1"></a></p>
<h3 id="the-class-of-an-object" class="section">3.4 The class of an object</h3>
<p><a href="" id="index-Classes"></a></p>
<p>All objects in R have a <em>class</em>, reported by the function <code class="calibre2">class</code>. For simple vectors this is just the mode, for example <code class="calibre2">"numeric"</code>, <code class="calibre2">"logical"</code>, <code class="calibre2">"character"</code> or <code class="calibre2">"list"</code>, but <code class="calibre2">"matrix"</code>, <code class="calibre2">"array"</code>, <code class="calibre2">"factor"</code> and <code class="calibre2">"data.frame"</code> are other possible values.</p>
<p>A special attribute known as the <em>class</em> of the object is used to allow for an object-oriented style<a href="appendix-f-references.html#FOOT13" id="DOCF13"><sup>13</sup></a> of programming in R. For example if an object has class <code class="calibre2">"data.frame"</code>, it will be printed in a certain way, the <code class="calibre2">plot()</code> function will display it graphically in a certain way, and other so-called generic functions such as <code class="calibre2">summary()</code> will react to it as an argument in a way sensitive to its class.</p>
<p>To remove temporarily the effects of class, use the function <code class="calibre2">unclass()</code>. <a href="" id="index-unclass"></a> For example if <code class="calibre2">winter</code> has the class <code class="calibre2">"data.frame"</code> then</p>
<div class="example">
<pre class="example1"><code>> winter</code></pre>
</div>
<p>will print it in data frame form, which is rather like a matrix, whereas</p>
<div class="example">
<pre class="example1"><code>> unclass(winter)</code></pre>
</div>
<p>will print it as an ordinary list. Only in rather special situations do you need to use this facility, but one is when you are learning to come to terms with the idea of class and generic functions.</p>
<p>Generic functions and classes will be discussed further in <a href="writing-your-own-functions.html#Object-orientation">Object orientation</a>, but only briefly.</p>
<hr />
<p><a href="" id="Factors"></a> <a href="" id="Ordered-and-unordered-factors"></a></p>
<div id="calibre_pb_10" class="calibre8">
</div>