forked from griffinp/r-novice-owls
-
Notifications
You must be signed in to change notification settings - Fork 1
/
06-data-subsetting.html
407 lines (406 loc) · 31.4 KB
/
06-data-subsetting.html
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: R for reproducible scientific analysis</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-responsive.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="stylesheet" type="text/css" href="css/swc-workshop-and-lesson.css" />
<link rel="stylesheet" type="text/css" href="css/lesson.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container container-full-width card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<div class="row-fluid">
<div class="span10 offset1">
<h1 class="title">R for reproducible scientific analysis</h1>
<h2 class="subtitle">Subsetting data</h2>
<div id="learning-objectives" class="objectives">
<h3>Learning Objectives</h3>
<ul>
<li>To be able to subset vectors, factors, matrices, lists, and data frames</li>
<li>To be able to extract individual and multiple elements:
<ul>
<li>by index,</li>
<li>by name,</li>
<li>using comparison operations</li>
</ul></li>
<li>To be able to skip and remove elements from various data structures.</li>
</ul>
</div>
<p>R has many powerful subset operators and mastering them will allow you to easily perform complex operations on any kind of dataset.</p>
<p>There are six different ways we can subset any kind of object, and three different subsetting operators for the different data structures.</p>
<p>Let's start with the workhorse of R: atomic vectors.</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">c</span>(<span class="fl">5.4</span>, <span class="fl">6.2</span>, <span class="fl">7.1</span>, <span class="fl">4.8</span>, <span class="fl">7.5</span>)
<span class="kw">names</span>(x) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>, <span class="st">'b'</span>, <span class="st">'c'</span>, <span class="st">'d'</span>, <span class="st">'e'</span>)</code></pre>
<p>So now that we've created a dummy vector to play with, how do we get at its contents?</p>
<h3 id="accessing-elements-using-their-indices">Accessing elements using their indices</h3>
<p>To extract elements of a vector we can give their corresponding index, starting from one:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="dv">1</span>]</code></pre>
<pre class="output"><code> a
5.4</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="dv">4</span>]</code></pre>
<pre class="output"><code> d
4.8 </code></pre>
<p>The square brackets operator is just like any other function. For atomic vectors (and matrices), it means "get me the nth element".</p>
<p>We can ask for multiple elements at once:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>)]</code></pre>
<pre class="output"><code> a c
5.4 7.1</code></pre>
<p>Or slices of the vector:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="dv">1</span>:<span class="dv">4</span>]</code></pre>
<pre class="output"><code> a b c d
5.4 6.2 7.1 4.8 </code></pre>
<p>the <code>:</code> operator just creates a sequence of numbers from the left element to the right. I.e. <code>x[1:4]</code> is equivalent to <code>x[c(1,2,3,4)]</code>.</p>
<p>We can ask for the same element multiple times:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">3</span>)]</code></pre>
<pre class="output"><code> a a c
5.4 5.4 7.1 </code></pre>
<p>If we ask for a number outside of the vector, R will return missing values:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="dv">6</span>]</code></pre>
<pre class="output"><code><NA>
NA</code></pre>
<p>This is a vector of length one containing an <code>NA</code>, whose name is also <code>NA</code>.</p>
<p>If we ask for the 0th element, we get an empty vector:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="dv">0</span>]</code></pre>
<pre class="output"><code>named numeric(0)</code></pre>
<p>But what about negative values?</p>
<h3 id="skipping-and-removing-elements">Skipping and removing elements</h3>
<p>If we use a negative number, R will return every element <em>except</em> for the one specified:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[-<span class="dv">2</span>]</code></pre>
<pre class="output"><code> a c d e
5.4 7.1 4.8 7.5 </code></pre>
<p>We can skip multiple elements:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(-<span class="dv">1</span>, -<span class="dv">5</span>)] <span class="co"># or x[-c(1,5)]</span></code></pre>
<pre class="output"><code> b c d
6.2 7.1 4.8 </code></pre>
<div id="tip-order-of-operations" class="callout">
<h4>Tip: Order of operations</h4>
<p>A common trip up for novices occurs when trying to skip slices of a vector. Most people first try to negate a sequence like so:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[-<span class="dv">1</span>:<span class="dv">3</span>]</code></pre>
<p>This gives a somewhat cryptic error:</p>
<pre class="output"><code>Error in x[-1:3] : only 0's may be mixed with negative subscripts</code></pre>
<p>But remember the order of operations. <code>:</code> is really a function, so what happens is it takes its first argument as -1, and second as 3, so generates the sequence of numbers: <code>c(-1, 0, 1, 2, 3)</code>.</p>
<p>The correct solution is to wrap that function call in brackets, so that the <code>-</code> operator applies to the results:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[-(<span class="dv">1</span>:<span class="dv">3</span>)]</code></pre>
<pre class="output"><code> d e
4.8 7.5 </code></pre>
</div>
<p>To remove elements from a vector, we need to assign the results back into the variable:</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span>x[-<span class="dv">4</span>]
x</code></pre>
<pre class="output"><code> a b c e
5.4 6.2 7.1 7.5 </code></pre>
<div id="challenge" class="challenge">
<h4>Challenge</h4>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">c</span>(<span class="fl">5.4</span>, <span class="fl">6.2</span>, <span class="fl">7.1</span>, <span class="fl">4.8</span>, <span class="fl">7.5</span>)
<span class="kw">names</span>(x) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>, <span class="st">'b'</span>, <span class="st">'c'</span>, <span class="st">'d'</span>, <span class="st">'e'</span>)
<span class="kw">print</span>(x)</code></pre>
<pre class="output"><code> a b c d e
5.4 6.2 7.1 4.8 7.5</code></pre>
<ol style="list-style-type: decimal">
<li>Come up with at least 3 different commands that will produce the following output:</li>
</ol>
<pre class="output"><code> b c d
6.2 7.1 4.8 </code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Compare notes with your neighbour. Did you have different strategies?</li>
</ol>
</div>
<h3 id="subsetting-by-name">Subsetting by name</h3>
<p>We can extract elements by using their name, instead of index:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"c"</span>)]</code></pre>
<pre class="output"><code> a c
5.4 7.1 </code></pre>
<p>This is usually a much more reliable way to subset objects: the position of various elements can often change when chaining together subsetting operations, but the names will always remain the same!</p>
<p>Unfortunately we can't skip or remove elements so easily.</p>
<p>To skip (or remove) a single named element:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[-<span class="kw">which</span>(<span class="kw">names</span>(x) ==<span class="st"> "a"</span>)<span class="er">)</span>]</code></pre>
<pre class="output"><code> b c e
6.2 7.1 7.5 </code></pre>
<p>The <code>which</code> function returns the indices of all <code>TRUE</code> elements of its argument. Remember that expressions evaluate before being passed to functions. Let's break this down so that its clearer whats happening.</p>
<p>First this happens:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(x) ==<span class="st"> "a"</span></code></pre>
<pre class="output"><code>[1] TRUE FALSE FALSE FALSE</code></pre>
<p>The condition operator is applied to every name of the vector <code>x</code>. Only the first name is "a" so that element is TRUE.</p>
<p><code>which</code> then converts this to an index:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">which</span>(<span class="kw">names</span>(x) ==<span class="st"> "a"</span>)</code></pre>
<pre class="output"><code>[1] 1</code></pre>
<p>Only the first element is <code>TRUE</code>, so `which returns 1. Now that we have indices the skipping works because we have a negative index!</p>
<p>Skipping multiple named indices is similar, but uses a different comparison operator:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[-<span class="kw">which</span>(<span class="kw">names</span>(x) %in%<span class="st"> </span><span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"c"</span>))]</code></pre>
<pre class="output"><code> b e
6.2 7.5 </code></pre>
<p>The <code>%in%</code> goes through each element of its left argument, in this case the names of <code>x</code>, and asks, "Does this element occur in the second argument?".</p>
<div id="tip-getting-help-for-operators" class="callout">
<h4>Tip: Getting help for operators</h4>
<p>Remember you can search for help on operators by wrapping them in quotes: <code>help("%in%")</code> or <code>?"%in%"</code>.</p>
</div>
<p>So why can't we use <code>==</code> like before? That's an excellent question.</p>
<p>Let's take a look at just the comparison component:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(x) ==<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>, <span class="st">'c'</span>)</code></pre>
<pre class="output"><code>[1] TRUE FALSE FALSE FALSE</code></pre>
<p>Obviously "c" is in the names of <code>x</code>, so why didn't this work? <code>==</code> works slightly differently to <code>%in%</code>. It will compare each element of its left argument to the corresponding element of its right argument.</p>
<p>Here's a mock illustration:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>, <span class="st">"e"</span>) <span class="co"># names of x</span>
|<span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="co"># The elements == is comparing</span>
<span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"c"</span>)</code></pre>
<p>When one vector is shorter than the other, it gets <em>recycled</em>:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>, <span class="st">"e"</span>) <span class="co"># names of x</span>
|<span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="er">|</span><span class="st"> </span><span class="co"># The elements == is comparing</span>
<span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"c"</span>, <span class="st">"a"</span>, <span class="st">"c"</span>)</code></pre>
<p>In this case R simply repeats <code>c("a", "c")</code> twice. If the longer vector length isn't a multiple of the shorter vector length, then R will also print out a warning message:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(x) ==<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>, <span class="st">'c'</span>, <span class="st">'e'</span>)</code></pre>
<pre class="output"><code>[1] TRUE FALSE FALSE FALSE
Warning message:
In names(x) == c("a", "c", "e") :
longer object length is not a multiple of shorter object length</code></pre>
<p>This difference between <code>==</code> and <code>%in%</code> is important to remember, because it can introduce hard to find and subtle bugs!</p>
<h3 id="subsetting-through-other-logical-operations">Subsetting through other logical operations</h3>
<p>We can also more simply subset through logical operations:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(<span class="ot">TRUE</span>, <span class="ot">TRUE</span>, <span class="ot">FALSE</span>, <span class="ot">FALSE</span>)]</code></pre>
<pre class="output"><code> a b
5.4 6.2 </code></pre>
<p>Note that in this case, the logical vector is also recycled to the length of the vector we're subsetting!</p>
<pre class="sourceCode r"><code class="sourceCode r">x[<span class="kw">c</span>(<span class="ot">TRUE</span>, <span class="ot">FALSE</span>)]</code></pre>
<pre class="output"><code> a c
5.4 7.1 </code></pre>
<p>Since comparison operators evaluate to logical vectors, we can also use them to succinctly subset vectors:</p>
<pre class="sourceCode r"><code class="sourceCode r">x[x ><span class="st"> </span><span class="dv">7</span>]</code></pre>
<pre class="output"><code> c e
7.1 7.5 </code></pre>
<div id="tip-chaining-logical-operations" class="callout">
<h4>Tip: Chaining logical operations</h4>
<p>There are many situations in which you will wish to combine multiple conditions. To do so several logical operations exist in R:</p>
<ul>
<li><code>|</code> logical OR: returns <code>TRUE</code>, if either the left or right are <code>TRUE</code>.</li>
<li><code>&</code> logical AND: returns <code>TRUE</code> if both the left and right are <code>TRUE</code></li>
<li><code>!</code> logical NOT: converts <code>TRUE</code> to <code>FALSE</code> and <code>FALSE</code> to <code>TRUE</code></li>
<li><code>&&</code> and <code>||</code> compare the individual elements of two vectors. Recycling rules also apply here.</li>
</ul>
</div>
<div id="challenge-1" class="challenge">
<h4>Challenge</h4>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">c</span>(<span class="fl">5.4</span>, <span class="fl">6.2</span>, <span class="fl">7.1</span>, <span class="fl">4.8</span>, <span class="fl">7.5</span>)
<span class="kw">names</span>(x) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">'a'</span>, <span class="st">'b'</span>, <span class="st">'c'</span>, <span class="st">'d'</span>, <span class="st">'e'</span>)
<span class="kw">print</span>(x)</code></pre>
<pre class="output"><code> a b c d e
5.4 6.2 7.1 4.8 7.5 </code></pre>
<ol style="list-style-type: decimal">
<li>Write a subsetting command to return the values in x that are greater than 4 and less than 7.</li>
</ol>
</div>
<h4 id="handling-special-values">Handling special values</h4>
<p>At some point you will encounter functions in R which cannot handle missing, infinite, or undefined data.</p>
<p>There are a number of special functions you can use to filter out this data:</p>
<ul>
<li><code>is.na</code> will return all positions in a vector, matrix, or data.frame containing <code>NA</code>.</li>
<li>likewise, <code>is.nan</code>, and <code>is.infinite</code> will do the same for <code>NaN</code> and <code>Inf</code>.</li>
<li><code>is.finite</code> will return all positions in a vector, matrix, or data.frame that do not contain <code>NA</code>, <code>NaN</code> or <code>Inf</code>.</li>
<li><code>na.omit</code> will filter out all missing values from a vector</li>
</ul>
<h3 id="factor-subsetting">Factor subsetting</h3>
<p>Now that we've explored the different ways to subset vectors, how do we subset the other data structures?</p>
<p>Factor subsetting works the same way as vector subsetting.</p>
<pre class="sourceCode r"><code class="sourceCode r">f <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">c</span>(<span class="st">"a"</span>, <span class="st">"a"</span>, <span class="st">"b"</span>, <span class="st">"c"</span>, <span class="st">"c"</span>, <span class="st">"d"</span>))
f[f ==<span class="st"> "a"</span>]
f[f %in%<span class="st"> </span><span class="kw">c</span>(<span class="st">"b"</span>, <span class="st">"c"</span>)]
f[<span class="dv">1</span>:<span class="dv">3</span>]</code></pre>
<p>An important note is that skipping elements will not remove the level even if no more of that category exists in the factor:</p>
<pre class="sourceCode r"><code class="sourceCode r">f[-<span class="dv">3</span>]</code></pre>
<pre class="output"><code>[1] a a c c d
Levels: a b c d</code></pre>
<h3 id="matrix-subsetting">Matrix subsetting</h3>
<p>Matrices are also subsetted using the <code>[</code> function. In this case it takes two arguments: the first applying to the rows, the second to its columns:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">set.seed</span>(<span class="dv">1</span>)
m <-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">rnorm</span>(<span class="dv">6</span>*<span class="dv">4</span>), <span class="dt">ncol=</span><span class="dv">4</span>, <span class="dt">nrow=</span><span class="dv">6</span>)
m[<span class="dv">3</span>:<span class="dv">4</span>, <span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">1</span>)]</code></pre>
<pre class="output"><code> [,1] [,2]
[1,] 1.12493092 -0.8356286
[2,] -0.04493361 1.5952808</code></pre>
<p>You can leave the first or second arguments blank to retrieve all the rows or columns respectively:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[, <span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">4</span>)]</code></pre>
<pre class="output"><code> [,1] [,2]
[1,] -0.62124058 0.82122120
[2,] -2.21469989 0.59390132
[3,] 1.12493092 0.91897737
[4,] -0.04493361 0.78213630
[5,] -0.01619026 0.07456498
[6,] 0.94383621 -1.98935170</code></pre>
<p>If we only access one row or column, R will automatically convert the result to a vector:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[<span class="dv">3</span>,]</code></pre>
<pre class="output"><code>[1] -0.8356286 0.5757814 1.1249309 0.9189774</code></pre>
<p>If you want to keep the output as a matrix, you need to specify a <em>third</em> argument; <code>drop = FALSE</code>:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[<span class="dv">3</span>, , drop=<span class="ot">FALSE</span>]</code></pre>
<pre class="output"><code> [,1] [,2] [,3] [,4]
[1,] -0.8356286 0.5757814 1.124931 0.9189774</code></pre>
<p>Unlike vectors, if we try to access a row or column outside of the matrix, R will throw an error:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[, <span class="kw">c</span>(<span class="dv">3</span>,<span class="dv">6</span>)]</code></pre>
<pre class="output"><code>Error in m[, c(3, 6)] : subscript out of bounds</code></pre>
<div id="tip-higher-dimensional-arrays" class="callout">
<h4>Tip: Higher dimensional arrays</h4>
<p>when dealing with multi-dimensional arrays, each argument to <code>[</code> corresponds to a dimension. For example, a 3D array, the first three arguments correspond to the rows, columns, and depth dimension.</p>
</div>
<p>Because matrices are really just vectors underneath the hood, we can also subset using only one argument:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[<span class="dv">5</span>]</code></pre>
<pre class="output"><code>[1] 0.3295078 </code></pre>
<p>This usually isn't useful. However it is useful to note that matrices are laid out in <em>column-major format</em> by default. That is the elements of the vector are arranged column-wise:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">matrix</span>(<span class="dv">1</span>:<span class="dv">6</span>, <span class="dt">nrow=</span><span class="dv">2</span>, <span class="dt">ncol=</span><span class="dv">3</span>)</code></pre>
<pre class="output"><code> [,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6</code></pre>
<p>Matrices can also be subsetted using their rownames and column names instead of their row and column indices.</p>
<div id="challenge-2" class="challenge">
<h4>Challenge</h4>
<pre class="sourceCode r"><code class="sourceCode r">m <-<span class="st"> </span><span class="kw">matrix</span>(<span class="dv">1</span>:<span class="dv">18</span>, <span class="dt">nrow=</span><span class="dv">3</span>, <span class="dt">ncol=</span><span class="dv">6</span>)
<span class="kw">print</span>(m)</code></pre>
<pre class="output"><code> [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 7 10 13 16
[2,] 2 5 8 11 14 17
[3,] 3 6 9 12 15 18</code></pre>
<ol style="list-style-type: decimal">
<li>Which of the following commands will extract the values 11 and 14?</li>
</ol>
<p>A. m[2,4,2,5]</p>
<p>B. m[2:5]</p>
<p>C. m[4:5,2]</p>
<p>D. m[2,c(4,5)]</p>
</div>
<h3 id="list-subsetting">List subsetting</h3>
<p>Now we'll introduce some new subsetting operators. There are three functions used to subset lists. <code>[</code>, as we've seen for atomic vectors and matrices, as well as <code>[[</code> and <code>$</code>.</p>
<p>Using <code>[</code> will always return a list. If you want to <em>subset</em> a list, but not <em>extract</em> an element, then you will likely use <code>[</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist <-<span class="st"> </span><span class="kw">list</span>(<span class="dt">a =</span> <span class="st">"Software Carpentry"</span>, <span class="dt">b =</span> <span class="dv">1</span>:<span class="dv">10</span>, <span class="dt">data =</span> <span class="kw">head</span>(iris))
xlist[<span class="dv">1</span>] </code></pre>
<p>This returns a <em>list with one element</em>:</p>
<pre class="output"><code>$a
[1] "Software Carpentry"
</code></pre>
<p>We can subset elements of a list exactly the same was as atomic vectors using <code>[</code>. Comparison operations however won't work as they're not recursive, they will try to condition on the data structures in each element of the list, not the individual elements within those data structures.</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist[<span class="dv">1</span>:<span class="dv">2</span>]</code></pre>
<pre class="output"><code>$a
[1] "Software Carpentry"
$b
[1] 1 2 3 4 5 6 7 8 9 10
</code></pre>
<p>To extract individual elements of a list, you need to use the double-square bracket function: <code>[[</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist[[<span class="dv">1</span>]]</code></pre>
<pre class="output"><code>[1] "Software Carpentry"</code></pre>
<p>Notice that now the result is a vector, not a list.</p>
<p>You can't extract more than one element at once:</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist[[<span class="dv">1</span>:<span class="dv">2</span>]]</code></pre>
<pre class="output"><code>Error in xlist[[1:2]] : subscript out of bounds</code></pre>
<p>Nor use it to skip elements:</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist[[-<span class="dv">1</span>]]</code></pre>
<pre class="output"><code>Error in xlist[[-1]] : attempt to select more than one element</code></pre>
<p>But you can use names to both subset and extract elements:</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist[[<span class="st">"a"</span>]]</code></pre>
<pre class="output"><code>[1] "Software Carpentry"</code></pre>
<p>The <code>$</code> function is a shorthand way for extracting elements by name:</p>
<pre class="sourceCode r"><code class="sourceCode r">xlist$data</code></pre>
<pre class="output"><code> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa</code></pre>
<div id="challenge-3" class="challenge">
<h4>Challenge</h4>
<ol style="list-style-type: decimal">
<li>Given the following list:</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">xlist <-<span class="st"> </span><span class="kw">list</span>(<span class="dt">a =</span> <span class="st">"Software Carpentry"</span>, <span class="dt">b =</span> <span class="dv">1</span>:<span class="dv">10</span>, <span class="dt">data =</span> <span class="kw">head</span>(iris)) </code></pre>
<p>Using your knowledge of both list and vector subsetting, extract the number 2 from xlist. Hint: the number 2 is contained within the "b" item in the list.</p>
<ol start="2" style="list-style-type: decimal">
<li>Given a linear model:</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">mod <-<span class="st"> </span><span class="kw">aov</span>(pop ~<span class="st"> </span>lifeExp, <span class="dt">data=</span>gapminder)</code></pre>
<p>Extract the residual degrees of freedom (hint: <code>attributes()</code> will help you)</p>
</div>
<h3 id="data-frames">Data frames</h3>
<p>Remember the data frames are lists underneath the hood, so similar rules apply. However they are also two dimensional objects:</p>
<p><code>[</code> with one argument will act the same was as for lists, where each list element corresponds to a column. The resulting object will be a data frame:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(gapminder[<span class="dv">3</span>])</code></pre>
<pre class="output"><code> pop
1 8425333
2 9240934
3 10267083
4 11537966
5 13079460
6 14880372</code></pre>
<p>Similarly, <code>[[</code> will act to extract <em>a single column</em>:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(gapminder[[<span class="st">"lifeExp"</span>]])</code></pre>
<pre class="output"><code>[1] 28.801 30.332 31.997 34.020 36.088 38.438</code></pre>
<p>And <code>$</code> provides a convenient shorthand to extact columns by name:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(gapminder$year)</code></pre>
<pre class="output"><code>[1] 1952 1957 1962 1967 1972 1977</code></pre>
<p>With two arguments, <code>[</code> behaves the same way as for matrices:</p>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[<span class="dv">1</span>:<span class="dv">3</span>,]</code></pre>
<pre class="output"><code> country year pop continent lifeExp gdpPercap
1 Afghanistan 1952 8425333 Asia 28.801 779.4453
2 Afghanistan 1957 9240934 Asia 30.332 820.8530
3 Afghanistan 1962 10267083 Asia 31.997 853.1007</code></pre>
<p>If we subset a single row, the result will be a data frame (because the elements are mixed types):</p>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[<span class="dv">3</span>,]</code></pre>
<pre class="output"><code> country year pop continent lifeExp gdpPercap
3 Afghanistan 1962 10267083 Asia 31.997 853.1007</code></pre>
<p>But for a single column the result will be a vector (this can be changed with the third argument, <code>drop = FALSE</code>).</p>
<div id="challenge-4" class="challenge">
<h4>Challenge</h4>
<p>Fix each of the following common data frame subsetting errors:</p>
<ol style="list-style-type: decimal">
<li>Extract observations collected for the year 1957</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[gapminder$year =<span class="st"> </span><span class="dv">1957</span>,]</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Extract all columns except 1 through to 4</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[,-<span class="dv">1</span>:<span class="dv">4</span>]</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>Extract the rows where the life expectancy is longer the 80 years</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[gapminder$lifeExp ><span class="st"> </span><span class="dv">80</span>]</code></pre>
<ol start="4" style="list-style-type: decimal">
<li>Extract the first row, and the fourth and fifth columns (<code>lifeExp</code> and <code>gdpPercap</code>).</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[<span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">5</span>]</code></pre>
<ol start="5" style="list-style-type: decimal">
<li>Advanced: extract rows that contain information for the years 2002 and 2007</li>
</ol>
<pre class="sourceCode r"><code class="sourceCode r">gapminder[gapminder$year ==<span class="st"> </span><span class="dv">2002</span> |<span class="st"> </span><span class="dv">2007</span>,]</code></pre>
</div>
<div id="challenge-5" class="challenge">
<h4>Challenge</h4>
<ol style="list-style-type: decimal">
<li><p>Why does <code>gapminder[1:20]</code> return an error? How does it differ from <code>gapminder[1:20, ]</code>?</p></li>
<li><p>Create a new <code>data.frame</code> called <code>gapminder_small</code> that only contains rows 1 through 9 and 19 through 23. You can do this in one or two steps.</p></li>
</ol>
</div>
</div>
</div>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/lesson-template">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="http://software-carpentry.org/v5/js/bootstrap/bootstrap.min.js"></script>
</body>
</html>