forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-supp-addressing-data.html
260 lines (258 loc) · 15.5 KB
/
01-supp-addressing-data.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Programming with R</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-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.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 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>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Programming with R</h1></a>
<h2 class="subtitle">Addressing data</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Understand the 3 different ways R can address data inside a data frame.</li>
<li>Combine different methods for addressing data with the assignment operator to update subsets of data</li>
</ul>
</div>
</section>
<p>R is a powerful language for data manipulation. There are 3 main ways for addressing data inside R objects.</p>
<ul>
<li>By index (slicing)</li>
<li>By logical vector</li>
<li>By name (columns only)</li>
</ul>
<p>Lets start by loading some sample data:</p>
<pre class="sourceCode r"><code class="sourceCode r">dat <-<span class="st"> </span><span class="kw">read.csv</span>(<span class="dt">file =</span> <span class="st">'data/sample.csv'</span>, <span class="dt">header =</span> <span class="ot">TRUE</span>, <span class="dt">stringsAsFactors =</span> <span class="ot">FALSE</span>)</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip"><span class="glyphicon glyphicon-pushpin"></span>Tip</h2>
</div>
<div class="panel-body">
<p>The first row of this csv file is a list of column names. We used the <em>header=TRUE</em> argument to <code>read.csv</code> so that R can interpret the file correctly. We are using the <em>stringsAsFactors=FALSE</em> argument to override the default behaviour for R. Using factors in R is covered in a separate lesson.</p>
</div>
</aside>
<p>Lets take a look at this data.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(dat)</code></pre>
<pre class="output"><code>[1] "data.frame"
</code></pre>
<p>R has loaded the contents of the .csv file into a variable called <code>dat</code> which is a <code>data frame</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">dim</span>(dat)</code></pre>
<pre class="output"><code>[1] 100 9
</code></pre>
<p>The data has 100 rows and 9 columns.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(dat)</code></pre>
<pre class="output"><code> ID Gender Group BloodPressure Age Aneurisms_q1 Aneurisms_q2
1 Sub001 m Control 132 16.0 114 140
2 Sub002 m Treatment2 139 17.2 148 209
3 Sub003 m Treatment2 130 19.5 196 251
4 Sub004 f Treatment1 105 15.7 199 140
5 Sub005 m Treatment1 125 19.9 188 120
6 Sub006 M Treatment2 112 14.3 260 266
Aneurisms_q3 Aneurisms_q4
1 202 237
2 248 248
3 122 177
4 233 220
5 222 228
6 320 294
</code></pre>
<p>The data is the results of an (not real) experiment, looking at the number of aneurisms that formed in the eyes of patients who undertook 3 different treatments.</p>
<h3 id="addressing-by-index">Addressing by Index</h3>
<p>Data can be accessed by index. We have already seen how square brackets <code>[</code> can be used to subset (slice) data. The generic format is <code>dat[row_numbers,column_numbers]</code>.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---selecting-values-1"><span class="glyphicon glyphicon-pencil"></span>Challenge - Selecting values 1</h2>
</div>
<div class="panel-body">
<p>What will be returned by <code>dat[1,1]</code>?</p>
</div>
</section>
<pre class="sourceCode r"><code class="sourceCode r">dat[<span class="dv">1</span>,<span class="dv">1</span>]</code></pre>
<pre class="output"><code>[1] "Sub001"
</code></pre>
<p>If we leave out a dimension R will interpret this as a request for all values in that dimension.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---selecting-values-2"><span class="glyphicon glyphicon-pencil"></span>Challenge - Selecting values 2</h2>
</div>
<div class="panel-body">
<p>What will be returned by <code>dat[,2]</code>?</p>
</div>
</section>
<p>The colon <code>:</code> can be used to create a sequence of integers.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">6</span>:<span class="dv">9</span></code></pre>
<pre class="output"><code>[1] 6 7 8 9
</code></pre>
<p>Creates a vector of numbers from 6 to 9.</p>
<p>This can be very useful for addressing data.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---subsetting-with-sequences"><span class="glyphicon glyphicon-pencil"></span>Challenge - Subsetting with sequences</h2>
</div>
<div class="panel-body">
<p>Use the colon operator to index just the aneurism count data (columns 6 to 9).</p>
</div>
</section>
<p>Finally we can use the <code>c()</code> (combine) function to address non-sequential rows and columns.</p>
<pre class="sourceCode r"><code class="sourceCode r">dat[<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">5</span>,<span class="dv">7</span>,<span class="dv">9</span>), <span class="dv">1</span>:<span class="dv">5</span>]</code></pre>
<pre class="output"><code> ID Gender Group BloodPressure Age
1 Sub001 m Control 132 16.0
5 Sub005 m Treatment1 125 19.9
7 Sub007 f Control 173 17.7
9 Sub009 m Treatment2 131 19.4
</code></pre>
<p>Returns the first 5 columns for patients in rows 1,5,7 & 9</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---subsetting-non-sequential-data"><span class="glyphicon glyphicon-pencil"></span>Challenge - Subsetting non-sequential data</h2>
</div>
<div class="panel-body">
<p>Return the Age and Gender values for the first 5 patients.</p>
</div>
</section>
<h3 id="addressing-by-name">Addressing by Name</h3>
<p>Columns in an R data frame are named.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(dat)</code></pre>
<pre class="output"><code>[1] "ID" "Gender" "Group" "BloodPressure"
[5] "Age" "Aneurisms_q1" "Aneurisms_q2" "Aneurisms_q3"
[9] "Aneurisms_q4"
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-1"><span class="glyphicon glyphicon-pushpin"></span>Tip</h2>
</div>
<div class="panel-body">
<p>If names are not specified e.g. using <code>headers=FALSE</code> in a <code>read.csv()</code> function, R assigns default names <code>V1,V2,...,Vn</code></p>
</div>
</aside>
<p>We usually use the <code>$</code> operator to address a column by name</p>
<pre class="sourceCode r"><code class="sourceCode r">dat$Gender</code></pre>
<pre class="output"><code> [1] "m" "m" "m" "f" "m" "M" "f" "m" "m" "f" "m" "f" "f" "m" "m" "m" "f"
[18] "m" "m" "F" "f" "m" "f" "f" "m" "M" "M" "f" "m" "f" "f" "m" "m" "m"
[35] "m" "f" "f" "m" "M" "m" "f" "m" "m" "m" "f" "f" "M" "M" "m" "m" "m"
[52] "f" "f" "f" "m" "f" "m" "m" "m" "f" "f" "f" "f" "M" "f" "m" "f" "f"
[69] "M" "m" "m" "m" "F" "m" "m" "f" "M" "M" "M" "f" "m" "M" "M" "m" "m"
[86] "f" "f" "f" "m" "m" "f" "m" "F" "f" "m" "m" "F" "m" "M" "M"
</code></pre>
<p>Named addressing can also be used in square brackets.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(dat[,<span class="kw">c</span>(<span class="st">'Age'</span>, <span class="st">'Gender'</span>)])</code></pre>
<pre class="output"><code> Age Gender
1 16.0 m
2 17.2 m
3 19.5 m
4 15.7 f
5 19.9 m
6 14.3 M
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="best-practice"><span class="glyphicon glyphicon-pushpin"></span>Best Practice</h2>
</div>
<div class="panel-body">
<p>Best practice is to address columns by name, often you will create or delete columns and the column position will change.</p>
</div>
</aside>
<h3 id="logical-indexing">Logical Indexing</h3>
<p>A logical vector contains only the special values <code>TRUE</code> & <code>FALSE</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r"><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>, <span class="ot">TRUE</span>)</code></pre>
<pre class="output"><code>[1] TRUE TRUE FALSE FALSE TRUE
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-2"><span class="glyphicon glyphicon-pushpin"></span>Tip</h2>
</div>
<div class="panel-body">
<p>Note the values <code>TRUE</code> and <code>FALSE</code> are all capital letters and are not quoted.</p>
</div>
</aside>
<p>Logical vectors can be created using <code>relational operators</code> e.g. <code><, >, ==, !=, %in%</code>.</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">11</span>, <span class="dv">12</span>, <span class="dv">13</span>)
x <<span class="st"> </span><span class="dv">10</span></code></pre>
<pre class="output"><code>[1] TRUE TRUE TRUE FALSE FALSE FALSE
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">x %in%<span class="st"> </span><span class="dv">1</span>:<span class="dv">10</span></code></pre>
<pre class="output"><code>[1] TRUE TRUE TRUE FALSE FALSE FALSE
</code></pre>
<p>We can use logical vectors to select data from a data frame.</p>
<pre class="sourceCode r"><code class="sourceCode r">index <-<span class="st"> </span>dat$Group ==<span class="st"> 'Control'</span>
dat[index,]$BloodPressure</code></pre>
<pre class="output"><code> [1] 132 173 129 77 158 81 137 111 135 108 133 139 126 125 99 122 155
[18] 133 94 98 74 116 97 104 117 90 150 116 108 102
</code></pre>
<p>Often this operation is written as one line of code:</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(dat[dat$Group ==<span class="st"> 'Control'</span>,]$BloodPressure)</code></pre>
<p><img src="fig/logical_vectors_indexing2-1.png" title="plot of chunk logical_vectors_indexing2" alt="plot of chunk logical_vectors_indexing2" style="display: block; margin: auto;" /></p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---using-logical-indexes"><span class="glyphicon glyphicon-pencil"></span>Challenge - Using logical indexes</h2>
</div>
<div class="panel-body">
<ol style="list-style-type: decimal">
<li>Create a scatterplot showing BloodPressure for subjects not in the control group.</li>
<li>How many ways are there to index this set of subjects?</li>
</ol>
</div>
</section>
<h3 id="combining-indexing-and-assignment">Combining Indexing and Assignment</h3>
<p>The assignment operator <code><-</code> can be combined with indexing.</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">11</span>, <span class="dv">12</span>, <span class="dv">13</span>)
x[x <<span class="st"> </span><span class="dv">10</span>] <-<span class="st"> </span><span class="dv">0</span>
x</code></pre>
<pre class="output"><code>[1] 0 0 0 11 12 13
</code></pre>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge---updating-a-subset-of-values"><span class="glyphicon glyphicon-pencil"></span>Challenge - Updating a subset of values</h2>
</div>
<div class="panel-body">
<p>In this dataset, values for Gender have been recorded as both uppercase <code>M, F</code> and lowercase <code>m,f</code>. Combine the indexing and assignment operations to convert all values to lowercase.</p>
</div>
</section>
</div>
</div>
</article>
<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/r-novice-inflammation">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="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37305346-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>