forked from swcarpentry/r-novice-gapminder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-rstudio-intro.html
354 lines (354 loc) · 26.5 KB
/
01-rstudio-intro.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
<!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>To gain familiarity with the various panes in the RStudio IDE</li>
<li>To gain familiarity with the buttons, short cuts and options in the RStudio IDE</li>
<li>To understand variables and how to assign to them</li>
<li>To be able to manage your workspace in an interactive R session</li>
<li>To be able to use mathematical and comparison operations</li>
<li>To be able to call functions</li>
<li>Introduction to package management</li>
</ul>
</div>
</section>
<h2 id="introduction-to-rstudio">Introduction to RStudio</h2>
<p>Welcome to the R portion of the Software Carpentry workshop.</p>
<p>Throughout this lesson, we’re going to teach you some of the fundamentals of the R language as well as some best practices for organising code for scientific projects that will make your life easier.</p>
<p>We’ll be using RStudio: a free, open source R integrated development environment. It provides a built in editor, works on all platforms (including on servers) and provides many advantages such as integration with version control and project management.</p>
<p><strong>Basic layout</strong></p>
<p>When you first open RStudio, you will be greeted by three panels:</p>
<ul>
<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>
</ul>
<p>Once you open files, such as R scripts, an editor panel will also open in the top left.</p>
<h2 id="work-flow-within-rstudio">Work flow within RStudio</h2>
<p>There are two main ways one can work within RStudio.</p>
<ol style="list-style-type: decimal">
<li>Test and play within the interactive R console then copy code into a .R file to run later.</li>
</ol>
<ul>
<li>This works well when doing small tests and initially starting off.</li>
<li>It quickly becomes laborious</li>
</ul>
<ol start="2" style="list-style-type: decimal">
<li>Start writing in an .R file and use RStudio’s command / short cut to push current line, selected lines or modified lines to the interactive R console.</li>
</ol>
<ul>
<li>This is a great way to start; all your code is saved for later</li>
<li>You will be able to run the file you create from within RStudio or using R’s <code>source()</code> function.</li>
</ul>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-running-segments-of-your-code"><span class="glyphicon glyphicon-pushpin"></span>Tip: Running segments of your code</h2>
</div>
<div class="panel-body">
<p>RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can 1. click on the <code>Run</code> button just above the editor panel, or 2. select “Run Lines” from the “Code” menu, or 3. hit Ctrl-Enter in Windows or Linux or Command-Enter on OS X. (This shortcut can also be seen by hovering the mouse over the button). To run a block of code, select it and then <code>Run</code>. If you have modified a line of code within a block of code you have just run, there is no need to reselct the section and <code>Run</code>, you can use the next button along, <code>Re-run the previous region</code>. This will run the previous code block inculding the modifications you have made.</p>
</div>
</aside>
<h2 id="introduction-to-r">Introduction to R</h2>
<p>Much of your time in R will be spent in the R interactive console. This is where you will run all of your code, and can be a useful environment to try out ideas before adding them to an R script file. This console in RStudio is the same as the one you would get if you just typed in <code>R</code> in your commandline environment.</p>
<p>The first thing you will see in the R interactive session is a bunch of information, followed by a “>” and a blinking cursor. In many ways this is similar to the shell environment you learned about during the shell lessons: it operates on the same idea of a “Read, evaluate, print loop”: you type in commands, R tries to execute them, and then returns a result.</p>
<h2 id="using-r-as-a-calculator">Using R as a calculator</h2>
<p>The simplest thing you could 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]”. Don’t worry about this for now, we’ll explain that later. For now think of it as indicating ouput.</p>
<p>Just like bash, 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 hit return 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.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-cancelling-commands"><span class="glyphicon glyphicon-pushpin"></span>Tip: Cancelling commands</h2>
</div>
<div class="panel-body">
<p>If you’re using R from the commandline instead of from within RStudio, you need to use <code>Ctrl+C</code> instead of <code>Esc</code> to cancel the command. This applies to Mac users as well!</p>
<p>Cancelling a command isn’t just useful for killing incomplete commands: you can also use it to tell R to stop running code (for example if its taking much longer than you expect), or to get rid of the code you’re currently writing.</p>
</div>
</aside>
<p>When using R as a calculator, the order of operations is the same as you would have learnt back in school.</p>
<p>From highest to lowest precedence:</p>
<ul>
<li>Parentheses: <code>(</code>, <code>)</code></li>
<li>Exponents: <code>^</code> or <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 operations in order to force the order of evaluation if it differs from the default, or to make clear what you intend.</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>
<p>This can get unwieldy when not needed, but clarifies your intentions. Remember that others may later read your code.</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"># clear, if you remember the rules</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"># if you forget some rules, this might help</span></code></pre></div>
<p>The text after each line of code is called a “comment”. Anything that follows after the hash (or octothorpe) symbol <code>#</code> is ignored by R when it executes code.</p>
<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">5e3</span> <span class="co"># Note the lack of minus here</span></code></pre></div>
<pre class="output"><code>[1] 5000
</code></pre>
<h2 id="mathematical-functions">Mathematical functions</h2>
<p>R has many built in mathematical functions. To call a function, we simply type its name, followed by open and closing parentheses. Anything we type inside the parentheses is called the function’s arguments:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">sin</span>(<span class="dv">1</span>) <span class="co"># trigonometry functions</span></code></pre></div>
<pre class="output"><code>[1] 0.841471
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log</span>(<span class="dv">1</span>) <span class="co"># natural logarithm</span></code></pre></div>
<pre class="output"><code>[1] 0
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">log10</span>(<span class="dv">10</span>) <span class="co"># base-10 logarithm</span></code></pre></div>
<pre class="output"><code>[1] 1
</code></pre>
<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>
<p>Don’t worry about trying to remember every function in R. You can simply look them up on google, or if you can remember the start of the function’s name, use the tab completion in RStudio.</p>
<p>This is one advantage that RStudio has over R on its own, it has autocompletion abilities that allow you to more easily look up functions, their arguments, and the values that they take.</p>
<p>Typing a <code>?</code> before the name of a command will open the help page for that command. As well as providing a detailed description of the command and how it works, scrolling ot the bottom of the help page will usually show a collection of code examples which illustrate command usage. We’ll go through an example later.</p>
<h2 id="comparing-things">Comparing things</h2>
<p>We can also do comparison in R:</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"># inequality (read as "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">1</span> <span class="co"># less than or 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">0</span> <span class="co"># greater 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>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-comparing-numbers"><span class="glyphicon glyphicon-pushpin"></span>Tip: Comparing Numbers</h2>
</div>
<div class="panel-body">
<p>A word of warning about comparing numbers: you should never use <code>==</code> to compare two numbers unless they are integers (a data type which can specifically represent only whole numbers).</p>
<p>Computers may only represent decimal numbers with a certain degree of precision, so two numbers which look the same when printed out by R, may actually have different underlying representations and therefore be different by a small margin of error (called Machine numeric tolerance).</p>
<p>Instead you should use the <code>all.equal</code> function.</p>
<p>Further reading: <a href="http://floating-point-gui.de/" class="uri">http://floating-point-gui.de/</a></p>
</div>
</aside>
<h2 id="variables-and-assignment">Variables and assignment</h2>
<p>We can store values in variables using the assignment operator <code><-</code>, like this:</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">40</span></code></pre></div>
<p>Notice that assignment does not print a value. Instead, we stored it for later in something called a <strong>variable</strong>. <code>x</code> now contains the <strong>value</strong> <code>0.025</code>:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x</code></pre></div>
<pre class="output"><code>[1] 0.025
</code></pre>
<p>More precisely, the stored value is a <em>decimal approximation</em> of this fraction called a <a href="http://en.wikipedia.org/wiki/Floating_point">floating point number</a>.</p>
<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"><span class="kw">log</span>(x)</code></pre></div>
<pre class="output"><code>[1] -3.688879
</code></pre>
<p>Notice also that variables can be reassigned:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">100</span></code></pre></div>
<p><code>x</code> used to contain the value 0.025 and and now it has the value 100.</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> <span class="co">#notice how RStudio updates its description of x on the top right tab</span></code></pre></div>
<p>The right hand side of the assignment can be any valid R expression. The right hand side is <em>fully evaluated</em> before the assignment occurs.</p>
<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, these include</p>
<ul>
<li>periods.between.words</li>
<li>underscores_between_words</li>
<li>camelCaseToSeparateWords</li>
</ul>
<p>What you use is up to you, but <strong>be consistent</strong>.</p>
<p>It is also possible to use the <code>=</code> operator for assignment:</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">40</span></code></pre></div>
<p>But this is much less common among R users. The most important thing is to <strong>be consistent</strong> with the operator you use. There are occasionally places where it is less confusing to use <code><-</code> than <code>=</code>, and it is the most common symbol used in the community. So the recommendation is to use <code><-</code>.</p>
<h2 id="vectorization">Vectorization</h2>
<p>One final thing to be aware of is that R is <em>vectorized</em>, meaning that variables and functions can have vectors as values. For example</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1</span>:<span class="dv">5</span></code></pre></div>
<pre class="output"><code>[1] 1 2 3 4 5
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="dv">2</span>^(<span class="dv">1</span>:<span class="dv">5</span>)</code></pre></div>
<pre class="output"><code>[1] 2 4 8 16 32
</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span><span class="dv">1</span>:<span class="dv">5</span>
<span class="dv">2</span>^x</code></pre></div>
<pre class="output"><code>[1] 2 4 8 16 32
</code></pre>
<p>This is incredibly powerful; we will discuss this further in an upcoming lesson.</p>
<h2 id="managing-your-environment">Managing your environment</h2>
<p>There are a few useful commands you can use to interact with the R session.</p>
<p><code>ls</code> will list all of the variables and functions stored in the global environment (your working R session):</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ls</span>()</code></pre></div>
<pre class="output"><code>[1] "hook_error" "hook_in" "hook_out" "x"
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-hidden-objects"><span class="glyphicon glyphicon-pushpin"></span>Tip: hidden objects</h2>
</div>
<div class="panel-body">
<p>Just like in the shell, <code>ls</code> will hide any variables or functions starting with a “.” by default. To list all objects, type <code>ls(all.names=TRUE)</code> instead</p>
</div>
</aside>
<p>Note here that we didn’t given any arguments to <code>ls</code>, but we still needed to give the parentheses to tell R to call the function.</p>
<p>If we type <code>ls</code> by itself, R will print out the source code for that function!</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">ls</code></pre></div>
<pre class="output"><code>function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE,
pattern, sorted = TRUE)
{
if (!missing(name)) {
pos <- tryCatch(name, error = function(e) e)
if (inherits(pos, "error")) {
name <- substitute(name)
if (!is.character(name))
name <- deparse(name)
warning(gettextf("%s converted to character string",
sQuote(name)), domain = NA)
pos <- name
}
}
all.names <- .Internal(ls(envir, all.names, sorted))
if (!missing(pattern)) {
if ((ll <- length(grep("[", pattern, fixed = TRUE))) &&
ll != length(grep("]", pattern, fixed = TRUE))) {
if (pattern == "[") {
pattern <- "\\["
warning("replaced regular expression pattern '[' by '\\\\['")
}
else if (length(grep("[^\\\\]\\[<-", pattern))) {
pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
warning("replaced '[<-' by '\\\\[<-' in regular expression pattern")
}
}
grep(pattern, all.names, value = TRUE)
}
else all.names
}
<bytecode: 0x7f851b927f40>
<environment: namespace:base>
</code></pre>
<p>You can use <code>rm</code> to delete objects you no longer need:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(x)</code></pre></div>
<p>If you have lots of things in your environment and want to delete all of them, you can pass the results of <code>ls</code> to the <code>rm</code> function:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(<span class="dt">list =</span> <span class="kw">ls</span>())</code></pre></div>
<p>In this case we’ve combined the two. Just like the order of operations, anything inside the innermost parentheses is evaluated first, and so on.</p>
<p>In this case we’ve specified that the results of <code>ls</code> should be used for the <code>list</code> argument in <code>rm</code>. When assigning values to arguments by name, you <em>must</em> use the <code>=</code> operator!!</p>
<p>If instead we use <code><-</code>, there will be unintended side effects, or you may just get an error message:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rm</span>(list <-<span class="st"> </span><span class="kw">ls</span>())</code></pre></div>
<pre class="error"><code>Error in rm(list <- ls()): ... must contain names or character strings
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-warnings-vs.errors"><span class="glyphicon glyphicon-pushpin"></span>Tip: Warnings vs. Errors</h2>
</div>
<div class="panel-body">
<p>Pay attention when R does something unexpected! Errors, like above, are thrown when R cannot proceed with a calculation. Warnings on the other hand usually mean that the function has run, but it probably hasn’t worked as expected.</p>
<p>In both cases, the message that R prints out usually give you clues how to fix a problem.</p>
</div>
</aside>
<h2 id="r-packages">R Packages</h2>
<p>It is possible to add functions to R by writing a package, or by obtaining a package written by someone else. As of this writing, there are over 7,000 packages available on CRAN (the comprehensive R archive network). R and RStudio have functionality for managing packages:</p>
<ul>
<li>You can see what packages are installed by typing <code>installed.packages()</code></li>
<li>You can install packages by typing <code>install.packages("packagename")</code>, where <code>packagename</code> is the package name, in quotes.</li>
<li>You can update installed packages by typing <code>update.packages()</code></li>
<li>You can remove a package with <code>remove.packages("packagename")</code></li>
<li>You can make a package available for use with <code>library(packagename)</code></li>
</ul>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge-1"><span class="glyphicon glyphicon-pencil"></span>Challenge 1</h2>
</div>
<div class="panel-body">
<p>What will be the value of each variable after each statement in the following program?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mass <-<span class="st"> </span><span class="fl">47.5</span>
age <-<span class="st"> </span><span class="dv">122</span>
mass <-<span class="st"> </span>mass *<span class="st"> </span><span class="fl">2.3</span>
age <-<span class="st"> </span>age -<span class="st"> </span><span class="dv">20</span></code></pre></div>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge-2"><span class="glyphicon glyphicon-pencil"></span>Challenge 2</h2>
</div>
<div class="panel-body">
<p>Run the code from the previous challenge, and write a command to compare mass to age. Is mass larger than age?</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge-3"><span class="glyphicon glyphicon-pencil"></span>Challenge 3</h2>
</div>
<div class="panel-body">
<p>Clean up your working environment by deleting the mass and age variables.</p>
</div>
</section>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="challenge-4"><span class="glyphicon glyphicon-pencil"></span>Challenge 4</h2>
</div>
<div class="panel-body">
<p>Install the following packages: <code>ggplot2</code>, <code>plyr</code>, <code>gapminder</code></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/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>