forked from jialiu232/R_basic_tutorial_2021Mar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_R_advanced.html
More file actions
835 lines (746 loc) · 24 KB
/
Copy pathtutorial_R_advanced.html
File metadata and controls
835 lines (746 loc) · 24 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Advanced R Tutorial</title>
<script src="site_libs/header-attrs-2.7/header-attrs.js"></script>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/sandstone.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<link rel="stylesheet" href="style.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-sm-12 col-md-4 col-lg-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-sm-12 col-md-8 col-lg-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="tutorial_R_basic.html">R Basic</a>
</li>
<li>
<a href="tutorial_R_advanced.html">Advanced R</a>
</li>
<li>
<a href="more_resources.html">More Resources</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Advanced R Tutorial</h1>
</div>
<hr />
<p>Author: Henri Chung, Jia Liu</p>
<p>Date: 03/27/2021</p>
<hr />
<p><br></p>
<div id="import-data-to-r" class="section level1" number="1">
<h1 number="1"><span class="header-section-number">1</span> Import data to R</h1>
<p>In practice, our data may often look like this:</p>
<table class="table" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
</th>
<th style="text-align:right;">
mpg
</th>
<th style="text-align:right;">
cyl
</th>
<th style="text-align:right;">
disp
</th>
<th style="text-align:right;">
hp
</th>
<th style="text-align:right;">
drat
</th>
<th style="text-align:right;">
wt
</th>
<th style="text-align:right;">
qsec
</th>
<th style="text-align:right;">
vs
</th>
<th style="text-align:right;">
am
</th>
<th style="text-align:right;">
gear
</th>
<th style="text-align:right;">
carb
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;">
Mazda RX4
</td>
<td style="text-align:right;">
21.0
</td>
<td style="text-align:right;">
6
</td>
<td style="text-align:right;">
160
</td>
<td style="text-align:right;">
110
</td>
<td style="text-align:right;">
3.90
</td>
<td style="text-align:right;">
2.620
</td>
<td style="text-align:right;">
16.46
</td>
<td style="text-align:right;">
0
</td>
<td style="text-align:right;">
1
</td>
<td style="text-align:right;">
4
</td>
<td style="text-align:right;">
4
</td>
</tr>
<tr>
<td style="text-align:left;">
Mazda RX4 Wag
</td>
<td style="text-align:right;">
21.0
</td>
<td style="text-align:right;">
6
</td>
<td style="text-align:right;">
160
</td>
<td style="text-align:right;">
110
</td>
<td style="text-align:right;">
3.90
</td>
<td style="text-align:right;">
2.875
</td>
<td style="text-align:right;">
17.02
</td>
<td style="text-align:right;">
0
</td>
<td style="text-align:right;">
1
</td>
<td style="text-align:right;">
4
</td>
<td style="text-align:right;">
4
</td>
</tr>
<tr>
<td style="text-align:left;">
Datsun 710
</td>
<td style="text-align:right;">
22.8
</td>
<td style="text-align:right;">
4
</td>
<td style="text-align:right;">
108
</td>
<td style="text-align:right;">
93
</td>
<td style="text-align:right;">
3.85
</td>
<td style="text-align:right;">
2.320
</td>
<td style="text-align:right;">
18.61
</td>
<td style="text-align:right;">
1
</td>
<td style="text-align:right;">
1
</td>
<td style="text-align:right;">
4
</td>
<td style="text-align:right;">
1
</td>
</tr>
<tr>
<td style="text-align:left;">
Hornet 4 Drive
</td>
<td style="text-align:right;">
21.4
</td>
<td style="text-align:right;">
6
</td>
<td style="text-align:right;">
258
</td>
<td style="text-align:right;">
110
</td>
<td style="text-align:right;">
3.08
</td>
<td style="text-align:right;">
3.215
</td>
<td style="text-align:right;">
19.44
</td>
<td style="text-align:right;">
1
</td>
<td style="text-align:right;">
0
</td>
<td style="text-align:right;">
3
</td>
<td style="text-align:right;">
1
</td>
</tr>
<tr>
<td style="text-align:left;">
Hornet Sportabout
</td>
<td style="text-align:right;">
18.7
</td>
<td style="text-align:right;">
8
</td>
<td style="text-align:right;">
360
</td>
<td style="text-align:right;">
175
</td>
<td style="text-align:right;">
3.15
</td>
<td style="text-align:right;">
3.440
</td>
<td style="text-align:right;">
17.02
</td>
<td style="text-align:right;">
0
</td>
<td style="text-align:right;">
0
</td>
<td style="text-align:right;">
3
</td>
<td style="text-align:right;">
2
</td>
</tr>
</tbody>
</table>
<p>Before talking about how to access values in such dataframes, let’s first see how we can import a dataset into R:</p>
<pre class="r"><code>?read.table()
ghg <- read.table("/Users/liujia/Desktop/europ_agriculture_GHG.csv", header = TRUE, sep = ",")</code></pre>
<pre class="r"><code># open the dataset in R
View(ghg)</code></pre>
<p>This is a dataset of greenhouse emission from agricultural area of European countries during Year <span class="math inline">\(1990\)</span>-<span class="math inline">\(2006\)</span>. The original dataset was downloaded from the <a href="https://data.europa.eu/euodp/en/data/dataset?q=air+emission&vocab_catalog=http%3A%2F%2Fdata.europa.eu%2F88u%2Fcatalog%2Feuodp&ext_boolean=all&sort=">European Union Open Data Portal</a>. I simplified the data so that it fits our workshop better.</p>
<p><br></p>
<p>Let’s take a quick look at the data:</p>
<pre class="r"><code># return the first parts of the object which is dataframe "ghg" here
head(ghg)</code></pre>
<pre><code>## Country_code Country Sector_code Sector Pollutant Year Value Units
## 1 AT Austria 4 Agriculture CH4 1990 230015.5 Mg
## 2 AT Austria 4 Agriculture CH4 1991 226798.0 Mg
## 3 AT Austria 4 Agriculture CH4 1992 218327.0 Mg
## 4 AT Austria 4 Agriculture CH4 1993 218808.7 Mg
## 5 AT Austria 4 Agriculture CH4 1994 219121.4 Mg
## 6 AT Austria 4 Agriculture CH4 1995 220144.6 Mg</code></pre>
<pre class="r"><code># return the last parts
tail(ghg)</code></pre>
<pre><code>## Country_code Country Sector_code Sector Pollutant Year Value Units
## 2171 TR Turkey 4 Agriculture N2O 2001 424.0000 Mg
## 2172 TR Turkey 4 Agriculture N2O 2002 444.0000 Mg
## 2173 TR Turkey 4 Agriculture N2O 2003 438.0000 Mg
## 2174 TR Turkey 4 Agriculture N2O 2004 484.0000 Mg
## 2175 TR Turkey 4 Agriculture N2O 2005 510.2228 Mg
## 2176 TR Turkey 4 Agriculture N2O 2006 510.2228 Mg</code></pre>
<pre class="r"><code># return the number of rows
nrow(ghg)</code></pre>
<pre><code>## [1] 2176</code></pre>
<pre class="r"><code># return the number of columns
ncol(ghg)</code></pre>
<pre><code>## [1] 8</code></pre>
<pre class="r"><code># return the dimensions
dim(ghg)</code></pre>
<pre><code>## [1] 2176 8</code></pre>
<blockquote>
<p>Notice that the datasets you work with in practical may not be clean, for example, there maybe missing values. <a href="https://uc-r.github.io/missing_values">Here</a> is a link that talks about how to deal with missing values in R. You may find more solutions or suggestions online.</p>
</blockquote>
<p><br></p>
</div>
<div id="data-wrangling-with-tidyverse" class="section level1" number="2">
<h1 number="2"><span class="header-section-number">2</span> Data wrangling with <code>tidyverse</code></h1>
<p><br></p>
<p>In the last Exercise, you applied one function <code>sort</code> to a dataframe. Sometimes, you may need to use multiple functions, say 10, to a dataframe before you get the final set that you like. Through this long process, you either store the output of each function to a variable and use it as an input of the next function, or use some function commands with data as the argument of the next function. Either way, it can be messy and easy to lose control of.</p>
<p>In <code>dplyr</code> package, there is a pipe operator <code>%>%</code>, which allows you to transfer the data or output of the previous function to be as the default input data of the next function:</p>
<pre class="r"><code>df_name %>%
function1( ... arguments) %>%
function2( ... arguments) %>%
function3( ... arguments)</code></pre>
<p>There are some very useful <code>dplyr</code> functions that can help us work with data. We will learn about some of them today.</p>
<hr />
<div id="select-columns-of-interest" class="section level2" number="2.1">
<h2 number="2.1"><span class="header-section-number">2.1</span> <strong>Select</strong> columns of interest</h2>
<p>Again with the <code>ghg</code> data:</p>
<pre class="r"><code>head(ghg)</code></pre>
<pre><code>## Country_code Country Sector_code Sector Pollutant Year Value Units
## 1 AT Austria 4 Agriculture CH4 1990 230015.5 Mg
## 2 AT Austria 4 Agriculture CH4 1991 226798.0 Mg
## 3 AT Austria 4 Agriculture CH4 1992 218327.0 Mg
## 4 AT Austria 4 Agriculture CH4 1993 218808.7 Mg
## 5 AT Austria 4 Agriculture CH4 1994 219121.4 Mg
## 6 AT Austria 4 Agriculture CH4 1995 220144.6 Mg</code></pre>
<p>We can know from the data description that all these data are from agriculture area, so the <code>Sector</code> and <code>Sector_code</code> columns are not really useful here. Also, we’ve already known that the <code>Units</code> for all the pollutant have all been unified to the same equivalance of <span class="math inline">\(CO_2\)</span>, so “Units” column doesn’t help explaining any specific information. I am not interested in “Country_code” column neither. So the columns that I am really interested are <code>Country</code>, <code>Pollutant</code>, <code>Year</code>, <code>Value</code>.</p>
<p>To select these columns:</p>
<pre class="r"><code>select(ghg, Country, Pollutant, Year, Value)
ghg %>%
select(Country, Pollutant, Year, Value) </code></pre>
<p>We can achieve the same goal by unselect the columns of not interested:</p>
<pre class="r"><code>ghg %>%
select(-c(Country_code, Sector, Sector_code, Units))</code></pre>
<blockquote>
<p><a href="https://suzan.rbind.io/2018/01/dplyr-tutorial-1/#selecting-columns-the-basics">More</a> options for selecting columns.</p>
</blockquote>
<p><br></p>
</div>
<div id="filter-rows-of-interest" class="section level2" number="2.2">
<h2 number="2.2"><span class="header-section-number">2.2</span> <strong>Filter</strong> rows of interest</h2>
<p>We may be interested in rows with specific patterns. For example, all the rows with <code>Pollutant == CO2</code>:</p>
<pre class="r"><code>ghg %>%
select(Country, Pollutant, Year, Value) %>%
filter(Pollutant == "CO2")</code></pre>
<p>You can filter out rows with <code>Year > 2000</code> by:</p>
<pre class="r"><code>ghg %>%
select(Country, Pollutant, Year, Value) %>%
filter(Year > 2000)</code></pre>
<p>In many situations, we may need to filter out rows with multiple conditions. For example, I can filter all the rows with <code>Year > 2000</code> and <code>Pollutant == "CO2"</code> by combining these two conditions with <code>&</code>:</p>
<pre class="r"><code>ghg %>%
select(Country, Pollutant, Year, Value) %>%
filter(Year > 2000 & Pollutant == "CO2")</code></pre>
<p><br></p>
<div id="exercise" class="section level3" number="2.2.1">
<h3 number="2.2.1"><span class="header-section-number">2.2.1</span> Exercise</h3>
<ul>
<li><p>You will use <code>ghg</code> as your input data, and use the <code>%>%</code> to pipe through functions like what we just did. So,</p>
<ul>
<li>Take <code>ghg</code> data, select columns <code>Country</code>, <code>Pollutant</code>, <code>Year</code>, <code>Value</code> with <code>select</code> function</li>
<li>Filter out rows with <code>Pollutant == N2O</code></li>
<li>Assign the values generated from above to variable <code>N2O_ghg</code> (Hint: use <code><-</code> to assign value to variable)</li>
</ul></li>
</ul>
<p>Solution:</p>
<pre class="r"><code>N2O_ghg <- ghg %>%
select(Country, Pollutant, Year, Value) %>%
filter(Pollutant == "N2O")</code></pre>
<p><br></p>
</div>
</div>
<div id="summarize-and-divide-data" class="section level2" number="2.3">
<h2 number="2.3"><span class="header-section-number">2.3</span> Summarize and Divide data</h2>
<p>You know how to do some manupulation to your data know. Some basic maths information may help you learn more about your data. <code>summarise()</code> function summarizes some maths such as sum, average, standard deviation, maximum, minimum, and counts of the columns you defined. To apply <code>summarise</code>, you can add your new column name, and let it equals to the math function with the variable. More than one summary functions are possible.</p>
<pre class="r"><code>N2O_ghg %>%
summarise(n=n(), average = mean(Value), std = sd(Value), maximum = max(Value))</code></pre>
<pre><code>## n average std maximum
## 1 544 30398.24 42287.36 202323.2</code></pre>
<p>That’s a big standard deviation. It kinda makes sense since it was calculated through all the countries. A lot of times, we want to summarize the information not through all the data but by a group, say <code>Country</code> in this case. <code>group_by</code> function will divide the data with the columns/variables that you specified, and the latter functions can be applied to each divided data section.</p>
<pre class="r"><code>N2O_ghg %>%
group_by(Country) %>%
summarise(average = mean(Value), std = sd(Value), maximum = max(Value)) </code></pre>
<pre><code>## # A tibble: 32 x 4
## Country average std maximum
## <chr> <dbl> <dbl> <dbl>
## 1 Austria 13390. 900. 14894.
## 2 Belgium 16981. 938. 17868.
## 3 Bulgaria 11711. 4051. 24298.
## 4 Cyprus 1037. 241. 1165.
## 5 Czech Republic 18907. 4110. 30931.
## 6 Denmark 23760. 3254. 29140.
## 7 Estonia 3165. 1302. 6231.
## 8 Finland 13321. 1039. 16018.
## 9 France 187117. 8057. 202323.
## 10 Germany 135265. 6463. 156105.
## # … with 22 more rows</code></pre>
<p>We just got some summaries of each European countries “N2O” gas emission throughout years. <code>arrange</code> function can be used to order a dataset based on values in one or multiple columns you defined. For example, I may want to order the dataset above based on firstly the “average”, and then “std” columns:</p>
<pre class="r"><code>N2O_ghg %>%
group_by(Country) %>%
summarise(average = mean(Value), std = sd(Value), maximum = max(Value)) %>%
arrange(desc(average), std)</code></pre>
<pre><code>## # A tibble: 32 x 4
## Country average std maximum
## <chr> <dbl> <dbl> <dbl>
## 1 France 187117. 8057. 202323.
## 2 Germany 135265. 6463. 156105.
## 3 United Kingdom 94891. 6837. 104129.
## 4 Italy 74424. 2530. 78243.
## 5 Spain 72686. 5701. 81671.
## 6 Poland 72536. 7934. 97904.
## 7 Romania 45278. 9637. 73284.
## 8 Netherlands 36396. 4219. 41262.
## 9 Greece 28968. 1739. 32453.
## 10 Ireland 24583. 1151. 26791.
## # … with 22 more rows</code></pre>
<p><code>arrange</code> will sort on ascending order in default. <code>arrange(desc(column_name))</code> will make it sort in descending order of the given column.</p>
<p><br></p>
</div>
</div>
<div id="visualizing-data-with-ggplot2" class="section level1" number="3">
<h1 number="3"><span class="header-section-number">3</span> Visualizing data with <code>ggplot2</code></h1>
<ul>
<li><p>syntax of ggplot2</p></li>
<li><p>plot the <code>ghg</code> data with ggplot2</p></li>
</ul>
<div id="exercise-1" class="section level2" number="3.1">
<h2 number="3.1"><span class="header-section-number">3.1</span> Exercise</h2>
<p><br></p>
</div>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>