-
Notifications
You must be signed in to change notification settings - Fork 9
/
01-intro-r-rstudio.html
362 lines (362 loc) · 27.4 KB
/
01-intro-r-rstudio.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
<!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-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">R for reproducible scientific analysis</h1></a>
<h2 class="subtitle">Introduction to R and RStudio</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>Appreciate why a social scientist might want to write code and why R specifically</li>
<li>Gain familiarity the RStudio IDE</li>
<li>Use basic math functions in R, with calculator and fun() notation</li>
<li>Understand variables and how to assign to them</li>
<li>Use comparison operations</li>
<li>Manage your workspace in an interactive R session</li>
<li>Understand errors, warnings, and messages</li>
<li>To be able to seek help via <code>?</code> and Google</li>
</ul>
</div>
</section>
<p>Welcome to the R portion of the workshop.</p>
<p>In this lesson, you’ll become familiar with the R language, the RStudio environment, and some best practices that will make your life easier and your science more reliable. You’ll also write a bit of R code.</p>
<h4 id="why-r">Why R?</h4>
<p>R is a free, open-source programming language that is designed for data analysis and statistics. It has a huge user-community and is highly extensible, with thousands of packages that add extra functionality at the official Comprehensive R Archive Network. For almost anything you want to do, there is an R package to help. In short, for most social scientists, it is the best tool to organize, visualize, and analyze data.</p>
<h4 id="why-rstudio">Why RStudio?</h4>
<p>RStudio is an IDE (integrated development environment) which we use to manage and execute R code. It is also free and open-source, it works on all platforms (e.g. you can use an Amazon Web Services cluster using RStudio), and it integrates version control and project management.</p>
<p>You write the same R code in RStudio as you would elsewhere, and it executes the same way. RStudio helps by keeping things nicely organized.</p>
<h4 id="why-code">Why Code?</h4>
<ul>
<li>Power</li>
<li>Flexibility</li>
<li>Editability: Error fixing, tweaks</li>
<li>Traceability: Return and know what was done</li>
<li>Reproducability: Cornerstone of science</li>
<li>Communication: No ambiguity</li>
</ul>
<h3 id="introduction-to-rstudio">Introduction to RStudio</h3>
<p>When you open RStudio you should see three panels:</p>
<ol style="list-style-type: decimal">
<li>The interactive R console (entire left)</li>
<li>Workspace/History (tabbed in upper right)</li>
<li>Files/Plots/Packages/Help (tabbed in lower right)</li>
</ol>
<h4 id="workflow-within-rstudio">Workflow within Rstudio</h4>
<p>Console vs. script</p>
<ul>
<li>Console
<ul>
<li>The R console is where code is run
<ul>
<li>The console in RStudio is the same as if typed in <code>R</code> in your command line</li>
</ul></li>
<li>When you start RStudio, you’ll see a bunch of information, followed by a “>” and a blinking cursor. This is a “Read, evaluate, print loop”. It’s highly interactive: You can type in commands and R will execute them and print the result.</li>
<li>You can work here, and your history is saved, but that is a laborious way to work</li>
</ul></li>
<li>Script
<ul>
<li>Preserve work in a plain-text file (with .R extension)</li>
<li>Create new R script with <code>File -> New File -> R Script</code> or ctrl/cmd-shift-N</li>
<li>There’s now a fourth RStudio panel, which is your plain-text script
<ul>
<li>Do your work here, and save this to be able to reproduce or edit it at a later date</li>
<li>For now your script is unsaved and called “untitled1” or something. We’ll fix that shortly</li>
</ul></li>
<li>cmd/ctrl-enter executes the line the cursor is on by copying that line and sending it to the Console
<ul>
<li>You can run multiple lines at once by highlighting them and pressing cmd/ctrl-enter</li>
</ul></li>
<li>Benefits of working in a script:
<ul>
<li>Mixes interactivity and preservation</li>
<li>Save just text and can get same results at another time or on another machine</li>
<li>Building preservable pipeline of operations</li>
</ul></li>
</ul></li>
</ul>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h4 id="tip-pushing-to-the-interactive-r-console"><span class="glyphicon glyphicon-pushpin"></span>Tip: Pushing to the interactive R console</h4>
</div>
<div class="panel-body">
<p>To run the line of your script where the cursor is, you can click on the <code>Run</code> button at the top-right of the script pane or use the keyboard short cut: cmd/ctrl-enter.</p>
<p>To run a block of code, select (highlight) it and click <code>Run</code> or cmd/ctrl-enter.</p>
<p>You are working toward selecting a whole script and running it.<br />
- You’ll write your script interactively, running each line to make sure it works, and at the end, you’ll be able to run the whole analysis by selecting all and running the script. This way you can later rerun the analysis on new or modified data or change part of the analysis and everything will work with the click of a button.</p>
</div>
</aside>
<h3 id="introduction-to-r">Introduction to R</h3>
<p>The simplest thing you can do with R is do arithmetic:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> +<span class="st"> </span><span class="dv">100</span></code></pre></div>
<pre class="output"><code>[1] 101
</code></pre>
<p>And R will print out the answer, with a preceding “[1]”, which indicates the first item of output.</p>
<p>If you type in an incomplete command, R will wait for you to complete it:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">><span class="st"> </span><span class="dv">1</span> -</code></pre></div>
<pre class="output"><code>+</code></pre>
<p>Any time you execute code and the R session shows a “+” instead of a “>”, it means it’s waiting for you to complete the command. If you want to cancel a command you can simply hit “Esc” and RStudio will give you back the “>” prompt. You can also cancel commands with “Esc” if R is taking too long to finish a calculation.</p>
<p>Order of operations works in R just like it did in algebra class. From highest to lowest precedence:</p>
<ul>
<li>Parentheses: <code>(</code>, <code>)</code></li>
<li>Exponents: <code>^</code></li>
<li>Divide: <code>/</code></li>
<li>Multiply: <code>*</code></li>
<li>Add: <code>+</code></li>
<li>Subtract: <code>-</code></li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">3</span> +<span class="st"> </span><span class="dv">5</span> *<span class="st"> </span><span class="dv">2</span></code></pre></div>
<pre class="output"><code>[1] 13
</code></pre>
<p>Use parentheses to group to force the order of evaluation, and/or to make code easier to read.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">(<span class="dv">3</span> +<span class="st"> </span><span class="dv">5</span>) *<span class="st"> </span><span class="dv">2</span></code></pre></div>
<pre class="output"><code>[1] 16
</code></pre>
<h4 id="whitespace">Whitespace</h4>
<p>Speaking of being easy to read, whitespace is ignored by R. Use it consistently to make code readable. For example, putting a single space on either side of an operator makes code easy to read.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">(<span class="dv">3</span> +<span class="st"> </span>(<span class="dv">5</span> *<span class="st"> </span>(<span class="dv">2</span> ^<span class="st"> </span><span class="dv">2</span>))) <span class="co"># hard to read</span>
<span class="dv">3</span> +<span class="st"> </span><span class="dv">5</span> *<span class="st"> </span><span class="dv">2</span> ^<span class="st"> </span><span class="dv">2</span> <span class="co"># easier to read, once you know rules</span>
<span class="dv">3+5</span>*<span class="dv">2</span>^<span class="dv">2</span> <span class="co"># very hard to read</span>
<span class="dv">3</span> +<span class="st"> </span><span class="dv">5</span> *<span class="st"> </span>(<span class="dv">2</span> ^<span class="st"> </span><span class="dv">2</span>) <span class="co"># to make order of operations clear, use parentheses</span></code></pre></div>
<h4 id="comments">Comments</h4>
<p>The text that appears to the right of each line of code above is called a comment. Anything that follows the hash symbol – <code>#</code> – is ignored by R.</p>
<p>Liberally add comments to your code as you write. Things that are clear as you write them will be mysterious to others, including your-future-self! Commenting takes little time and will save you time and headaches in the long run.</p>
<h4 id="scientific-notation">Scientific Notation</h4>
<p>Really small or large numbers get a scientific notation:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">2</span>/<span class="dv">10000</span></code></pre></div>
<pre class="output"><code>[1] 2e-04
</code></pre>
<p>Which is shorthand for “multiplied by <code>10^XX</code>”. So <code>2e-4</code> is shorthand for <code>2 * 10^(-4)</code>.</p>
<p>You can write numbers in scientific notation too:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="fl">1e9</span> <span class="co"># One billion</span></code></pre></div>
<pre class="output"><code>[1] 1e+09
</code></pre>
<h4 id="mathematical-functions">Mathematical functions</h4>
<p>R has many built in mathematical functions. To call a function, type its name, follow by open and closing parentheses. Anything we type inside those parentheses is an “argument” to that function.</p>
<p>Here we call the <code>sin</code> function and provide it the argument 3.14, or approximately <span class="math inline"><em>π</em></span>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">sin</span>(<span class="fl">3.14</span>) <span class="co"># trigonometry functions</span></code></pre></div>
<pre class="output"><code>[1] 0.001592653
</code></pre>
<p>We can take a logarith:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dv">3</span>) <span class="co"># natural logarithm</span></code></pre></div>
<pre class="output"><code>[1] 1.098612
</code></pre>
<p>Or exponentiate:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">exp</span>(<span class="fl">0.5</span>) <span class="co"># e^(1/2)</span></code></pre></div>
<pre class="output"><code>[1] 1.648721
</code></pre>
<h4 id="nested-functions">Nested Functions</h4>
<p>You can even put functions inside each other. <code>exp(0.5)</code> raised <code>e</code> to the <code>1/2</code> power. Equivalently we could take the square-root of <code>e</code>. Expressions are interpretted from the inside-out: In the following line, R first takes <code>e^1</code> (which is <code>e</code>), and then takes the square-root (that’s what the <code>sqrt</code> function does) of the result.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">sqrt</span>(<span class="kw">exp</span>(<span class="dv">1</span>))</code></pre></div>
<pre class="output"><code>[1] 1.648721
</code></pre>
<p>You don’t need to remember function names. There are many ways to discover or rediscover them when you need them. Google is your friend, but we will discuss other ways soon.</p>
<h4 id="comparison">Comparison</h4>
<p>We can do logical comparison in R. This will be important later, for example, when we want to filter a dataset based on a logical condition.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> ==<span class="st"> </span><span class="dv">1</span> <span class="co"># equality (note two equals signs, read as "is equal to")</span></code></pre></div>
<pre class="output"><code>[1] TRUE
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> !=<span class="st"> </span><span class="dv">2</span> <span class="co"># not-equal (read, "is not equal to")</span></code></pre></div>
<pre class="output"><code>[1] TRUE
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> <<span class="st"> </span><span class="dv">2</span> <span class="co"># less than</span></code></pre></div>
<pre class="output"><code>[1] TRUE
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span> >=<span class="st"> </span>-<span class="dv">9</span> <span class="co"># greater than or equal to</span></code></pre></div>
<pre class="output"><code>[1] TRUE
</code></pre>
<h4 id="variables-and-assignment">Variables and assignment</h4>
<p>We can store values in variables using the assignment operator <code><-</code>. You can also use a single equals sign, <code>=</code>, for assignment.</p>
<p>Note that unlike every other expression we have run so far, R doesn’t print anything when we run this next line. Instead, it is stored for later in a <strong>variable</strong>, <code>x</code>. <code>x</code> now contains the <strong>value</strong> <code>0.25</code>. Read this as “Assign 1/4 to x.”</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">1</span>/<span class="dv">4</span></code></pre></div>
<p>Look for the <code>Environment</code> tab in one of the panes of RStudio, and you will see that <code>x</code> and its value have appeared. Our variable <code>x</code> can be used in place of a number in any calculation that expects a number:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x</code></pre></div>
<pre class="output"><code>[1] 0.25
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(x)</code></pre></div>
<pre class="output"><code>[1] -1.386294
</code></pre>
<p>This doesn’t change the value of <code>x</code> or store the result anywhere, it simply prints the answer to the console.</p>
<p>Variables can be reassigned:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">99</span></code></pre></div>
<p><code>x</code> used to contain the value 0.25 and and now it has the value 99.</p>
<p>Assignment values can contain the variable being assigned to:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span>x +<span class="st"> </span><span class="dv">1</span> </code></pre></div>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="mcq-variable-assignment"><span class="glyphicon glyphicon-pencil"></span>MCQ – Variable Assignment</h4>
</div>
<div class="panel-body">
<p>What does the following code print?</p>
<pre><code>a <- 1
b <- 2
c <- a + b
b <- 4
a <- b
c <- a
c </code></pre>
<ol style="list-style-type: lower-alpha">
<li>a</li>
<li>3</li>
<li>4</li>
<li>::nothing::</li>
</ol>
</div>
</section>
<h4 id="variable-name-conventions">Variable name conventions</h4>
<p>Variable names can contain letters, numbers, underscores and periods. They cannot start with a number nor contain spaces at all. Different people use different conventions for long variable names, especially:</p>
<ul>
<li>underscores_between_words</li>
<li>camelCaseToSeparateWords</li>
</ul>
<p>What you use is up to you, but <strong>be consistent</strong>.</p>
<h4 id="tab-completion">Tab completion</h4>
<p>Use descriptive variable names, as they make your code easier to understand. It will save time because you’ll remember what each variable is: It’s easier to remember what <code>domesticPopulation</code> is than <code>dp</code> or <code>x</code>. A silly example:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">theNumberNine <-<span class="st"> </span><span class="dv">9</span></code></pre></div>
<p>Tab-completion is a really nice feature of RStudio that saves typing and avoid typos. After you assign 9 to <code>theNumberNine</code>, if you start typing <code>t...</code>, <code>th...</code>, etc., and then pressing <code>tab</code>, RStudio will pull up a box of all the valid ways to finish that word. You can scroll through them using the up- and down-arrows and press enter to choose the one you want. If you press tab when there is only one valid way to complete something, RStudio will automatically complete it.</p>
<h3 id="understanding-functions-getting-help">Understanding functions & getting help</h3>
<h4 id="r-help-files">R help files</h4>
<p>Once you figure out what function you want, you need to figure out how to use it. Every function has an associated help-file. They can be hard to read, especially at first, but it is important to learn how to make sense of them.</p>
<p><code>?function</code> brings up help-file. E.g.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">?log</code></pre></div>
<p>Each help-file contains the following components.</p>
<ul>
<li>Description: An extended description of what the function does.</li>
<li>Usage: The arguments of the function and their default values.</li>
<li>Arguments: An explanation of the data each argument is expecting.</li>
<li>Details: Any important details to be aware of.</li>
<li>Value: The data the function returns.</li>
<li>See Also: Any related functions you might find useful.</li>
<li>Examples: Some examples for how to use the function.</li>
</ul>
<h4 id="other-ways-to-get-help">Other ways to get help</h4>
<ul>
<li><code>??</code> searches the text of all R help files, e.g. <code>??base</code> will find <code>log</code>.</li>
<li>Google</li>
<li>Stack Overflow</li>
<li><a href="http://www.cookbook-r.com/">Cookbook for R</a></li>
<li><a href="http://www.rstudio.com/resources/cheatsheets/">RStudio cheat sheets</a></li>
</ul>
<h4 id="arguments-to-functions">Arguments to functions</h4>
<ul>
<li>Can be specified by order or by name</li>
<li>Before, when we entered <code>log(3)</code>, <code>log</code> knew <code>3</code> was <code>x</code> because it was in the first position, but we could have also told <code>log</code> explicitly that <code>3</code> is the value <code>x</code> should take. These are the same:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dv">3</span>) </code></pre></div>
<pre class="output"><code>[1] 1.098612
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">x =</span> <span class="dv">3</span>)</code></pre></div>
<pre class="output"><code>[1] 1.098612
</code></pre>
<ul>
<li>Some arguments have default values, e.g. <code>log</code>’s <code>base</code> defaults to <code>exp(1)</code>, <em>e</em>, unless you tell it otherwise. So these are identical:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">x =</span> <span class="dv">3</span>)</code></pre></div>
<pre class="output"><code>[1] 1.098612
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">x =</span> <span class="dv">3</span>, <span class="dt">base =</span> <span class="kw">exp</span>(<span class="dv">1</span>))</code></pre></div>
<pre class="output"><code>[1] 1.098612
</code></pre>
<p>To get the base 10 logarithm of 3, you could do</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">x =</span> <span class="dv">3</span>, <span class="dt">base =</span> <span class="dv">10</span>)</code></pre></div>
<pre class="output"><code>[1] 0.4771213
</code></pre>
<p>If you provide a function with arguments by name, they can go in any order. Otherwise, they have to appear in the order specified by the function. These are all the same:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dv">3</span>, <span class="dv">10</span>)</code></pre></div>
<pre class="output"><code>[1] 0.4771213
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">x =</span> <span class="dv">3</span>, <span class="dt">base =</span> <span class="dv">10</span>)</code></pre></div>
<pre class="output"><code>[1] 0.4771213
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dt">base =</span> <span class="dv">10</span>, <span class="dt">x =</span> <span class="dv">3</span>)</code></pre></div>
<pre class="output"><code>[1] 0.4771213
</code></pre>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="mcq-which-of-these-things-is-not-like-the-other-ones"><span class="glyphicon glyphicon-pencil"></span>MCQ – Which of these things is not like the other ones?</h4>
</div>
<div class="panel-body">
<p>Three of the following lines produce the same result. Without running the code, which one will produce a different result than the others? The helpfile for <code>log</code> (<code>?log</code>) may be helpful.</p>
<pre><code>a. log(x = 1000, base = 10)
b. log10(1000)
c. log(base = 10, x = 1000)
d. log(10, 1000)</code></pre>
</div>
</section>
<h3 id="when-r-wants-to-tell-you-something">When R Wants to Tell You Something</h3>
<p>Besides the value of an expression R has executed, there are a few other kinds of responses you might get from R, including errors, warnings, and messages.</p>
<h4 id="errors">Errors</h4>
<p>R returns an error when it cannot proceed. It stops you in your tracks. The error message will provide some information on what the problem was, but it is often cryptic. Learning to understand these messages is important but takes practice. Here’s an example of an error:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">log_of_a_word <-<span class="st"> </span><span class="kw">log</span>(<span class="st">"a_word"</span>)</code></pre></div>
<pre class="output"><code>Error in log("a_word"): non-numeric argument to mathematical function
</code></pre>
<p>R tell us that something has gone wrong: It got a non-number for a function that needs a number. Note that errors prevent execution of the line, so nothing got assigned to <code>log_of_a_word</code> there. If we ask R what it thinks <code>log_of_a_word</code> is, it will return another error. Practice understanding R’s communication style: Do you understand how R is telling you what the problem is?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">log_of_a_word</code></pre></div>
<pre class="output"><code>Error in eval(expr, envir, enclos): object 'log_of_a_word' not found
</code></pre>
<h4 id="warnings">Warnings</h4>
<p>Warnings appear in the same red font in the console, but they start with “Warning” instead of “Error”. Warnings are R’s way of telling you that it did something, but it suspects it may not have been what you wanted. <em>Warnings can be more insidious than errors</em> because you can keep going, but keep going with a mistake in your pipeline. Here’s an example:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">log_of_a_negative <-<span class="st"> </span><span class="kw">log</span>(-<span class="dv">2</span>)</code></pre></div>
<pre class="output"><code>Warning in log(-2): NaNs produced
</code></pre>
<p><code>NaN</code> means “not a number”, and R has kindly told us, “Hey, I think you probably wanted a number here – taking a log of a negative is kind of a weird thing to do. I can do it if you really want, I just want to be make sure it’s what you want.”</p>
<p>Note that it did work, so if we ask R what <code>log_of_a_negative</code> is, we won’t get an error. Note that we don’t get a warning either, so you need to pay attention when warnings first appear.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">log_of_a_negative</code></pre></div>
<pre class="output"><code>[1] NaN
</code></pre>
<h4 id="messages">Messages</h4>
<p>There’s a third source of red text in R: messages. These are R’s way of telling you that something happened, but it’s probably nothing to worry about. These don’t start with “Message”; they just print the red text. We can make R print one like this:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">message</span>(<span class="st">"Hey buddy!"</span>)</code></pre></div>
<pre class="output"><code>Hey buddy!
</code></pre>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h4 id="challenge-extra-practice-assignment-comparison"><span class="glyphicon glyphicon-pencil"></span>Challenge – Extra practice: Assignment & Comparison</h4>
</div>
<div class="panel-body">
<p>Which elephant weighs more? Convert one’s weight to the units of the other, and store the result in an appropriately-named new variable. Write a command to test whether elephant1 weights more than elephant2 (1 kg ≈ 2.2 lb).</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">elephant1_kg <-<span class="st"> </span><span class="dv">3492</span>
elephant2_lb <-<span class="st"> </span><span class="dv">7757</span></code></pre></div>
</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/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="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>