forked from resbaz/r-novice-gapminder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path09-vectorisation.html
151 lines (151 loc) · 8.69 KB
/
09-vectorisation.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
<!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">Vectorisation</h2>
<div id="learning-objectives" class="objectives">
<h2>Learning Objectives</h2>
<ul>
<li>To understand vectorised operations in R.</li>
</ul>
</div>
<p>One of the nice features of R is that most of its functions are vectorized, that is the function will operate on all elements of a vector without needing to loop through and act on each element one at a time. This makes writing code more concise, easy to read, and less error prone.</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">1</span>:<span class="dv">4</span>
x *<span class="st"> </span><span class="dv">2</span></code></pre>
<pre class="output"><code>[1] 2 4 6 8</code></pre>
<p>The multiplication happened to each element of the vector.</p>
<p>We can also add two vectors together:</p>
<pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span><span class="dv">6</span>:<span class="dv">9</span>
x +<span class="st"> </span>y</code></pre>
<pre class="output"><code>[1] 7 9 11 13</code></pre>
<p>Each element of <code>x</code> was added to its corresponding element of <code>y</code>:</p>
<pre class="output"><code>x: 1 2 3 4
+ + + +
y: 6 7 8 9
---------------
7 9 11 13</code></pre>
<p>Just as we saw in the previous lesson, vectors will recycle:</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">3</span>)</code></pre>
<pre class="output"><code>[1] 2 5 4 7</code></pre>
<p>Like so:</p>
<pre class="output"><code>x: 1 2 3 4
+ + + +
y: 1 3 1 3
---------------
2 5 4 7</code></pre>
<p>Comparison operators also apply element-wise, as we saw in the subsetting lesson:</p>
<pre class="sourceCode r"><code class="sourceCode r">x ><span class="st"> </span><span class="dv">2</span></code></pre>
<pre class="output"><code>[1] FALSE FALSE TRUE TRUE</code></pre>
<p>Logical operations are also vectorised:</p>
<pre class="sourceCode r"><code class="sourceCode r">a <-<span class="st"> </span>x ><span class="st"> </span><span class="dv">3</span>
b <-<span class="st"> </span>x <<span class="st"> </span><span class="dv">3</span></code></pre>
<pre class="sourceCode r"><code class="sourceCode r">a</code></pre>
<pre class="output"><code>[1] FALSE FALSE FALSE TRUE</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">b</code></pre>
<pre class="output"><code>[1] TRUE TRUE FALSE FALSE</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">a |<span class="st"> </span>b </code></pre>
<pre class="output"><code>[1] TRUE TRUE FALSE TRUE</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">a &<span class="st"> </span>b</code></pre>
<div id="tip-some-useful-functions-for-logical-vectors" class="callout">
<h4>Tip: some useful functions for logical vectors</h4>
<p><code>any()</code> will return <code>TRUE</code> if any element of a vector is <code>TRUE</code> <code>all()</code> will return <code>TRUE</code> if <em>all</em> elements of a vector are <code>TRUE</code></p>
</div>
<pre class="output"><code>[1] FALSE FALSE FALSE FALSE</code></pre>
<p>Many functions also operate on element-wise on vectors:</p>
<pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">1</span>:<span class="dv">4</span>
<span class="kw">sin</span>(x)</code></pre>
<pre class="output"><code>[1] 0.8414710 0.9092974 0.1411200 -0.7568025</code></pre>
<p>Vectorised operations also work element wise on matrices:</p>
<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">12</span>, <span class="dt">nrow=</span><span class="dv">3</span>, <span class="dt">ncol=</span><span class="dv">4</span>)
m *<span class="st"> </span>-<span class="dv">1</span></code></pre>
<pre class="output"><code> [,1] [,2] [,3] [,4]
[1,] -1 -4 -7 -10
[2,] -2 -5 -8 -11
[3,] -3 -6 -9 -12</code></pre>
<p>Note that <code>*</code> gives you element-wise multiplication!</p>
<pre class="sourceCode r"><code class="sourceCode r">m *<span class="st"> </span>m</code></pre>
<pre class="output"><code> [,1] [,2] [,3] [,4]
[1,] 1 16 49 100
[2,] 4 25 64 121
[3,] 9 36 81 144</code></pre>
<p>To do matrix multiplication, we need to use the <code>%*%</code> operator:</p>
<pre class="sourceCode r"><code class="sourceCode r">m %*%<span class="st"> </span><span class="kw">t</span>(m) <span class="co"># The t() function returns the transpose of a matrix</span></code></pre>
<pre class="ouput"><code> [,1] [,2] [,3]
[1,] 166 188 210
[2,] 188 214 240
[3,] 210 240 270</code></pre>
<p>For more on matrix algebra, see the <a href="http://www.statmethods.net/advstats/matrix.html">Quick-R reference guide</a></p>
<div id="challenge-1" class="challenge">
<h4>Challenge 1</h4>
<p>Create a subset of the <code>gapminder</code> dataset countaining entries only for Australia.</p>
<p>Calculate the mean GDP (GDP per capita multiplied by total population) over all years on record</p>
</div>
<div id="challenge-2" class="challenge">
<h4>Challenge 2</h4>
<p>We're interested in looking at the convergence of the following sequence of fractions:</p>
<pre class="output"><code> 1/(1^2) + 1/(2^2) + 1/(3^2) + ... + 1/(n^2)</code></pre>
<p>This would be tedious to type out, and impossible for large numbers of N.</p>
<ol style="list-style-type: decimal">
<li>generate a vector of numbers from 1 to 100 and square them.</li>
<li>Take the inverse of that vector.</li>
<li>Sum them (hint: <code>sum</code> will add together all elements in a vector).</li>
<li>Do the same thing, but for numbers 1 to 10,000</li>
</ol>
</div>
<div id="challenge-3" class="challenge">
<h4>Challenge 3</h4>
<p>Given the following matrix:</p>
<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">12</span>, <span class="dt">nrow=</span><span class="dv">3</span>, <span class="dt">ncol=</span><span class="dv">4</span>)
m</code></pre>
<pre class="output"><code> [,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12</code></pre>
<p>Write down what you think will happen when you run:</p>
<ol style="list-style-type: decimal">
<li><code>m ^ -1</code></li>
<li><code>m * c(1, 0, -1)</code></li>
<li><code>m > c(0, 20)</code></li>
</ol>
<p>Did you get the output expected? If not, ask a helper!</p>
</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:admin@software-carpentry.org">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>