-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBasic.html
More file actions
executable file
·1303 lines (861 loc) · 41.2 KB
/
Copy pathRBasic.html
File metadata and controls
executable file
·1303 lines (861 loc) · 41.2 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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>Quick R Introduction</title>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="generator" content="pandoc" />
<meta name="date" content="2018-05-22" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<base target="_blank">
<script type="text/javascript">
var SLIDE_CONFIG = {
// Slide settings
settings: {
title: 'Quick R Introduction',
useBuilds: true,
usePrettify: true,
enableSlideAreas: true,
enableTouch: true,
},
// Author information
presenters: [
{
name: 'DSP作者群 <span class="citation">@BenQ</span> Workshop' ,
company: '',
gplus: '',
twitter: '',
www: '',
github: ''
},
]
};
</script>
<link href="RBasic_files/ioslides-13.5.1/fonts/fonts.css" rel="stylesheet" />
<link href="RBasic_files/ioslides-13.5.1/theme/css/default.css" rel="stylesheet" />
<link href="RBasic_files/ioslides-13.5.1/theme/css/phone.css" rel="stylesheet" />
<script src="RBasic_files/ioslides-13.5.1/js/modernizr.custom.45394.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/prettify/prettify.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/prettify/lang-r.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/prettify/lang-yaml.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/hammer.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/slide-controller.js"></script>
<script src="RBasic_files/ioslides-13.5.1/js/slide-deck.js"></script>
<style type="text/css">
b, strong {
font-weight: bold;
}
em {
font-style: italic;
}
slides > slide {
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
.auto-fadein {
-webkit-transition: opacity 0.6s ease-in;
-webkit-transition-delay: 0.4s;
-moz-transition: opacity 0.6s ease-in 0.4s;
-o-transition: opacity 0.6s ease-in 0.4s;
transition: opacity 0.6s ease-in 0.4s;
opacity: 0;
}
</style>
<link rel="stylesheet" href="css/dsp.css" type="text/css" />
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
<p style="margin-top: 6px; margin-left: -2px;">2018-05-22</p>
</hgroup>
</slide>
<slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch01:為何要學 R 語言?</h2></hgroup><article id="ch01-r-">
</article></slide><slide class=''><hgroup><h2>R 是專門為資料分析而設計的程式語言</h2></hgroup><article id="r-" class="smaller centered">
<img src='img/statician_10521919-655x280.jpg' style='width: 960px'></img>
<h3>
R 可以執行大多數的統計計算、機器學習、資料採礦方法
</h3>
<p>取自 <a href='http://goo.gl/CkXlvY' title=''>http://goo.gl/CkXlvY</a></p>
</article></slide><slide class=''><hgroup><h2>免費、開源、豐沛的社群資源</h2></hgroup><article class="smaller columns-2 centered">
<img src='img/fig_8_cran.png' style='height: 400px'></img>
<h3>
很容易擴充和客製化
</h3>
<p>取自 <a href='http://goo.gl/AwfHLx' title=''>http://goo.gl/AwfHLx</a></p>
<img src='img/r-integration-1.png' style='height: 400px'></img>
<h3>
很容易和其他工具做整合
</h3>
<p>取自 <a href='http://goo.gl/jvNnG3' title=''>http://goo.gl/jvNnG3</a></p>
</article></slide><slide class=''><hgroup><h2>R 可以輸出高品質的視覺化</h2></hgroup><article id="r-" class="smaller centered">
<p><img src='img/flights_sml.jpg' style='width: 960px'></img></p>
<p>取自 <a href='http://goo.gl/q1NX26' title=''>http://goo.gl/q1NX26</a></p>
</article></slide><slide class=''><hgroup><h2>課程目標</h2></hgroup><article >
<ul style="font-size: 120%">
<li>
建立 R 的使用環境
</li>
<li>
熟悉 R 語言基礎操作
</li>
<li>
了解 R 語言的物件的結構
</li>
<li>
體會 R 語言的流程控制
</li>
<li>
學習 R 語言的資料整理
</li>
<li>
確立 R 語言的資料爬析概念
</li>
</ul>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch02:建立 R 的使用環境</h2></hgroup><article id="ch02-r-">
</article></slide><slide class=''><hgroup><h2>傢俬準備好</h2></hgroup><article class="columns-2">
<h3>環境安裝</h3>
<ul>
<li>主程式:<a href='https://www.r-project.org/' title=''>R</a>(R-3.3.3 以上版本)</li>
<li>編輯界面:<a href='https://www.rstudio.com/products/rstudio/' title=''>RStudio IDE</a>(0.99.473 以上版本)</li>
<li><a href='https://github.com/dspim/R/wiki/R-&-RStudio-Troubleshooting-Guide' title=''>疑難排解指南</a> <br> <br> <br></li>
</ul>
<h3>RStudio 界面說明</h3>
<ul>
<li>程式碼編輯區</li>
<li>命令列區</li>
<li>環境資訊區</li>
<li>檔案系統區</li>
</ul>
</article></slide><slide class=''><hgroup><h2>熟悉 RStudio 的 <strong>命令列</strong> 界面</h2></hgroup><article id="-rstudio---">
<h3>程式的<strong>輸入</strong>、<strong>輸出</strong>、<strong>中斷</strong></h3>
<ul>
<li>左下角的當符號 <code>></code> 表示可以輸入指令</li>
<li>輸入 <code>1 + 1</code> 後按下 <strong>Enter</strong>,檢查螢幕輸出</li>
<li>輸入 <code>1 +</code> 後按下 <strong>Enter</strong>,檢查螢幕輸出 <br> 最左下角的開頭變成 <code>+</code> 表示尚未輸入完成,應繼續輸入</li>
<li>按下 <strong>ESC</strong>,會中斷執行中的程式 (左下角回復成 <code>></code> 開頭)</li>
</ul>
</article></slide><slide class=''><hgroup><h2>熟悉 RStudio 的 <strong>程式碼編輯</strong> 界面</h2></hgroup><article id="-rstudio---">
<h3>停留時間最多的區域</h3>
<ul>
<li><strong>New File -> R Script -> Untiled1.R</strong></li>
<li>在程式碼編輯區中輸入 <code>1 + 1</code> 後按下 <strong>Control + Enter</strong>,檢查 命令列區</li>
<li>在程式碼編輯區中輸入 <code>1 +</code> 後按下 <strong>Control + Enter</strong>,檢查 命令列區</li>
<li>在命令列區按下 <strong>ESC</strong> 中斷程式</li>
</ul>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch03 今天學完,你一定會用R做資料整理</h2></hgroup><article id="ch03-r">
</article></slide><slide class=''><hgroup><h2>先別想,複製貼上就是了</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>dat1 <- data.frame(date=c("11/29","11/30","12/01","12/02","12/03","12/04","12/05"),
weekday=c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))
dat1 # print R object</pre>
<pre > date weekday
1 11/29 Sun
2 11/30 Mon
3 12/01 Tue
4 12/02 Wed
5 12/03 Thu
6 12/04 Fri
7 12/05 Sat</pre>
<pre class = 'prettyprint lang-r'>dat1[1, 1] # 取值
dat1[, 1]
dat1[1:2,] </pre>
</article></slide><slide class=''><hgroup><h2>先別想,複製貼上就是了</h2></hgroup><article id="-1">
<pre class = 'prettyprint lang-r'>dat2 <- data.frame(date=c("11/29","11/30","12/01","12/02","12/03","12/04","12/05"),
temp=c(17, 18, 24, 20, 21, 22, 24))
dat2 # print R object</pre>
<pre > date temp
1 11/29 17
2 11/30 18
3 12/01 24
4 12/02 20
5 12/03 21
6 12/04 22
7 12/05 24</pre>
<pre class = 'prettyprint lang-r'>dat2[, 2]
dat2[, 2] * 2
dat2[, 2] + c(1, 0, -0.5, 2, 3, -2, 0.5) </pre>
</article></slide><slide class=''><hgroup><h2>先別想,複製貼上就是了</h2></hgroup><article id="-2">
<pre class = 'prettyprint lang-r'>dat2[dat2$temp<20,]</pre>
<pre > date temp
1 11/29 17
2 11/30 18</pre>
<pre class = 'prettyprint lang-r'>dat2[grepl("11", dat2$date), ]</pre>
<pre > date temp
1 11/29 17
2 11/30 18</pre>
<pre class = 'prettyprint lang-r'>cbind(dat1,temp=dat2$temp)[dat2$temp<20,]</pre>
<pre > date weekday temp
1 11/29 Sun 17
2 11/30 Mon 18</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch04:基礎教學 - 敘述句與數列</h2></hgroup><article id="ch04---">
</article></slide><slide class=''><hgroup><h2>敘述句</h2></hgroup><article class="columns-2">
<pre class = 'prettyprint lang-r'>1</pre>
<pre >[1] 1</pre>
<pre class = 'prettyprint lang-r'>2</pre>
<pre >[1] 2</pre>
<pre class = 'prettyprint lang-r'>1; 2</pre>
<pre >[1] 1</pre>
<pre >[1] 2</pre>
<br>
<p>
<br>
<p>
<p>
<h3>筆記</h3>
<ul>
<li>灰底的區塊為程式碼 (輸入),[1] 為運算結果 (輸出)</li>
<li>敘述句以<strong>斷行 (Enter)</strong> 或<strong>分號 ( ; )</strong> 作結尾</li>
<li>未完成的敘述句,命令列開頭會變成 <code>+</code></li>
<li>可以用 <strong>Esc</strong> 中斷敘述句</li>
<li>R 會把單引號 <code>'apple'</code> 或雙引號 <code>"book"</code> 所包覆的敘述當成字串</li>
</ul>
</article></slide><slide class=''><hgroup><h2>敘述句2</h2></hgroup><article id="2" class="columns-2">
<pre class = 'prettyprint lang-r'># 基礎運算
1 + 2 + 3</pre>
<pre >[1] 6</pre>
<pre class = 'prettyprint lang-r'>1 + 2 + 3</pre>
<pre >[1] 6</pre>
<pre class = 'prettyprint lang-r'>x <- 10
y <- 4
(x + y) / 2 # 簡單的公式運算</pre>
<pre >[1] 7</pre>
<br>
<p>
<p>
<p><br></p>
<h3>筆記</h3>
<ul>
<li>基本數學運算符號 (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, …)</li>
<li>空白 (<strong>space</strong>) 不影響程式執行</li>
<li>箭號 (<code><-</code>) 表示賦值,把箭號右邊的結果指向到R物件</li>
<li>井號 (<strong>#</strong>) 表示註解,使得該行不執行運算</li>
<li>在RStudio中,想要一次註解多行可用 <strong>ctrl + shift + c</strong></li>
<li>在命令列中按 <strong>ctrl + l</strong> 可以清除當前畫面</li>
</ul>
</article></slide><slide class=''><hgroup><h2>最基礎的物件:數值型向量 (數列)</h2></hgroup><article id="-" class="columns-2">
<pre class = 'prettyprint lang-r'># basic expression of integer vector
c(1, 2, 3, 4)</pre>
<pre >[1] 1 2 3 4</pre>
<pre class = 'prettyprint lang-r'># simple expression
1:4</pre>
<pre >[1] 1 2 3 4</pre>
<pre class = 'prettyprint lang-r'>4:1</pre>
<pre >[1] 4 3 2 1</pre>
<br>
<p>
<p>
<h3>筆記</h3>
<ul>
<li>以 <code>c(...)</code> 表示 (c 取自combine之意), 整個函數內容以 <code>()</code>包括起來,元素以逗號分隔。</li>
<li>連續整數向量可以利用 <code>:</code> (冒號) 簡記,譬如數列 (1, 2, 3, 4) 在 R 語言中可利用 <code>1:4</code> 簡記</li>
<li>也可以使用 <code>4:1</code> 的簡記方式來產生 (4, 3, 2, 1) 的向量</li>
</ul>
</article></slide><slide class=''><hgroup><h2>如何生成有序的數值向量</h2></hgroup><article class="columns-2">
<pre class = 'prettyprint lang-r'>seq(1, 4)</pre>
<pre >[1] 1 2 3 4</pre>
<pre class = 'prettyprint lang-r'>seq(1, 9, by = 2) # 間隔為2</pre>
<pre >[1] 1 3 5 7 9</pre>
<pre class = 'prettyprint lang-r'>seq(1, 9, length.out = 5) # 分割長度為5</pre>
<pre >[1] 1 3 5 7 9</pre>
<p>
<p>
<p>
<p>
<p>
<br>
<p>
<h3>筆記</h3>
<ul>
<li>除了冒號簡記法外,可以透過<code>seq</code>函數生成有規則的數值向量(序列)<br/></li>
<li>在<code>seq()</code> 函數中按 <strong>tab</strong> 鍵觀察有哪些參數可以使用</li>
<li><code>by</code> 表示數列間隔,預設為1</li>
<li><code>length.out</code> 表示數列長度</li>
</ul>
</article></slide><slide class=''><hgroup><h2>小挑戰</h2></hgroup><article >
<ul>
<li>利用簡記法列出 1 ~ 10的數列</li>
<li>利用 <code>seq</code> 函數列出偶數數列: 2, 4, 6, 8, 10</li>
<li>觀察 <code>seq(1, 10, length.out=5)</code> 的輸出結果
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p></li>
</ul>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article id="section">
<p>參考解答</p>
<pre class = 'prettyprint lang-r'>1:10</pre>
<pre > [1] 1 2 3 4 5 6 7 8 9 10</pre>
<pre class = 'prettyprint lang-r'>seq(2, 10, by = 2)</pre>
<pre >[1] 2 4 6 8 10</pre>
<pre class = 'prettyprint lang-r'>seq(2, 10, length.out = 5)</pre>
<pre >[1] 2 4 6 8 10</pre>
<pre class = 'prettyprint lang-r'>seq(1, 10, length.out=5)</pre>
<pre >[1] 1.00 3.25 5.50 7.75 10.00</pre>
</div>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch05:基礎教學 - 數列的運算</h2></hgroup><article id="ch05---">
</article></slide><slide class=''><hgroup><h2>數列的運算</h2></hgroup><article class="columns-2">
<p>R的數列運算滿足 <strong>recycling properties</strong></p>
<pre class = 'prettyprint lang-r'>c(1, 2, 3) * c(2, 2, 2)</pre>
<pre >[1] 2 4 6</pre>
<pre class = 'prettyprint lang-r'># shorter arguments are recycled
1:3 * 2</pre>
<pre >[1] 2 4 6</pre>
<pre class = 'prettyprint lang-r'>c(0.5, 1.5, 2.5, 3.5) * c(2, 1)</pre>
<pre >[1] 1.0 1.5 5.0 3.5</pre>
<p>
<p>
<p>
<p>
<p>
<h3>小挑戰</h3>
<p>向量的四則運算,請計算以下五位女藝人的BMI</p>
<p>Hint: BMI = 體重 / (身高/100)^2</p>
<pre class = 'prettyprint lang-r'>height <- c(174, 158, 160, 168, 173)
weight <- c(52, 39, 42, 46, 48)</pre>
<aside class='note'><section><pre class = 'prettyprint lang-r'>height <- c(174, 158, 160, 168, 173)
weight <- c(52, 39, 42, 46, 48)
weight/ (height/100)^2</pre>
<pre >[1] 17.17532 15.62250 16.40625 16.29819 16.03796</pre></section></aside>
</article></slide><slide class=''><hgroup><h2>向量的取值</h2></hgroup><article class="columns-2">
<ul>
<li>在<code>[ ]</code> (中括號) 中輸入元素的位置進行取值</li>
<li>使負號 (<code>-</code>) 移除給定位置元素 (反向選取)</li>
</ul>
<pre class = 'prettyprint lang-r'>x <- c(174, 158, 160, 168, 173)
x[1] # 選取第1個位置的元素</pre>
<pre >[1] 174</pre>
<pre class = 'prettyprint lang-r'>x[c(1, 3)] # 選取第1, 3個位置的元素</pre>
<pre >[1] 174 160</pre>
<pre class = 'prettyprint lang-r'>x[c(2, 3, 1)] # 依序取值</pre>
<pre >[1] 158 160 174</pre>
<pre class = 'prettyprint lang-r'># 在[ ]中使用負號 (-) 做反向選取
x[-1]</pre>
<pre >[1] 158 160 168 173</pre>
<pre class = 'prettyprint lang-r'>x[-c(1, 3, 4)]</pre>
<pre >[1] 158 173</pre>
</article></slide><slide class=''><hgroup><h2>向量的取值2</h2></hgroup><article id="2" class="columns-2">
<p>給定條件進行取值</p>
<ul>
<li>比較運算子(<code>></code>, <code><</code>, <code>>=</code>, <code><=</code>, <code>==</code>, <code>!=</code>)</li>
<li>邏輯運算子 (<code>&</code>, <code>|</code>)</li>
</ul>
<pre class = 'prettyprint lang-r'>x > 160</pre>
<pre >[1] TRUE FALSE FALSE TRUE TRUE</pre>
<pre class = 'prettyprint lang-r'># 使用比較運算子 加上 `which` 函數進行取值
index <- which(x > 160)
index </pre>
<pre >[1] 1 4 5</pre>
<pre class = 'prettyprint lang-r'>x[index]</pre>
<pre >[1] 174 168 173</pre>
<pre class = 'prettyprint lang-r'># 指令壓縮,將指令寫在 [ ] 中,以達到縮短程式碼的功效
x[which(x > 160)]</pre>
<pre >[1] 174 168 173</pre>
<pre class = 'prettyprint lang-r'># 配合邏輯算子進行多條件的取值
x[which(x > 160 & x < 175)]</pre>
<pre >[1] 174 168 173</pre>
<pre class = 'prettyprint lang-r'># 可以省略 which
x[x > 160 & x < 175]</pre>
<pre >[1] 174 168 173</pre>
</article></slide><slide class=''><hgroup><h2>向量的取代</h2></hgroup><article class="columns-2">
<ul>
<li>利用 <code>[ ]</code> (中括號) 與 <code><-</code> (箭號) 進行取代與新增元素</li>
</ul>
<pre class = 'prettyprint lang-r'>x <- c(174, 158, 160, 168, 173)
# 取代特定位置的元素
x[2] <- 158.5 # 取代x物件的第二個元素
x</pre>
<pre >[1] 174.0 158.5 160.0 168.0 173.0</pre>
<pre class = 'prettyprint lang-r'>x[c(1, 5)] <- 175
x</pre>
<pre >[1] 175.0 158.5 160.0 168.0 175.0</pre>
<p>
<pre class = 'prettyprint lang-r'># 也可以用條件篩選做取代
x[x > 160] <- 170 # 取代大於160的值為170
x</pre>
<pre >[1] 170.0 158.5 160.0 170.0 170.0</pre>
</article></slide><slide class=''><hgroup><h2>向量新增</h2></hgroup><article class="columns-2">
<ul>
<li>可用 <code>[ ]</code> (中括號) 與 <code><-</code> (箭號) 進行新增元素<br/></li>
<li><code>NA</code> 為系統保留字,表示Not Available / Missing Values</li>
</ul>
<pre class = 'prettyprint lang-r'>x <- c(174, 158, 160, 168, 173)
# 在 [ ] 中新增元素
x[6] <- 168
x</pre>
<pre >[1] 174 158 160 168 173 168</pre>
<pre class = 'prettyprint lang-r'>x[8] <- 147
x # 未指定的元素值預設為NA</pre>
<pre >[1] 174 158 160 168 173 168 NA 147</pre>
<pre class = 'prettyprint lang-r'>length(x) # 查看向量物件的長度</pre>
<pre >[1] 8</pre>
<pre class = 'prettyprint lang-r'>x[length(x) + 1] <- 166 # 接續增加新元素</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch06:查詢說明檔</h2></hgroup><article id="ch06">
</article></slide><slide class=''><hgroup><h2>在 R 中查詢說明文件</h2></hgroup><article id="-r-" class="columns-2">
<p>各種自救措施</p>
<pre class = 'prettyprint lang-r'>help.start()
ab # 輸入`ab`後 按下tab
?abs # 等同於 help(abs)
??abs
vignette()
vignette("Introduction", "Matrix")</pre>
<br> <br>
<p>
<br> <br> <br> <br>
<p>
<p>
<h3>筆記</h3>
<ul>
<li>help.start: 說明檔首頁</li>
<li>自動完成 (tab鍵):列出所有<code>ab</code>開頭的函數</li>
<li>? (一個問號):查詢特定函數的說明檔</li>
<li>?? (兩個問號):查詢包含特定關鍵字的說明檔</li>
<li>apropos : 查詢包含特定關鍵字的函數</li>
<li>example : 執行特定函數的使用範例</li>
<li>vignette : 查詢R環境中各種介紹文件</li>
</ul>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch07:資料測量的尺度</h2></hgroup><article id="ch07">
</article></slide><slide class=''><hgroup><h2>R 的資料形態分類</h2></hgroup><article id="r-">
<table class = 'rmdtable'>
<tr class="header">
<th align="left">資料衡量尺度</th>
<th align="left">R變數形態</th>
<th align="left">特性</th>
<th align="left">範例</th>
</tr>
<tr class="odd">
<td align="left">連續資料</td>
<td align="left">numeric</td>
<td align="left">數值</td>
<td align="left">身高</td>
</tr>
<tr class="even">
<td align="left">比例資料</td>
<td align="left">numeric</td>
<td align="left">比值</td>
<td align="left">流失率</td>
</tr>
<tr class="odd">
<td align="left">區間資料</td>
<td align="left">numeric</td>
<td align="left">大小距離</td>
<td align="left">溫度</td>
</tr>
<tr class="even">
<td align="left">順序資料</td>
<td align="left">factor</td>
<td align="left">優先順序</td>
<td align="left">名次</td>
</tr>
<tr class="odd">
<td align="left">名目資料</td>
<td align="left">factor</td>
<td align="left">類別</td>
<td align="left">國家</td>
</tr>
<tr class="even">
<td align="left">布林資料</td>
<td align="left">logical</td>
<td align="left">邏輯值</td>
<td align="left">性別</td>
</tr>
<tr class="odd">
<td align="left">文字資料</td>
<td align="left">character</td>
<td align="left">文字</td>
<td align="left">電話號碼</td>
</tr>
</table>
</article></slide><slide class=''><hgroup><h2>判斷 Logical</h2></hgroup><article id="-logical">
<p>產生自比較,或是使用<code>T</code>、<code>TRUE</code>、<code>F</code>或<code>FALSE</code>輸入</p>
<pre class = 'prettyprint lang-r'>x <- 1 # 賦值
x < 2</pre>
<pre >[1] TRUE</pre>
<pre class = 'prettyprint lang-r'>x <= 1</pre>
<pre >[1] TRUE</pre>
</article></slide><slide class=''><hgroup><h2>字串 (Character)</h2></hgroup><article id="-character" class="columns-2">
<ul>
<li>輸入的時候利用<code>"</code>或<code>'</code>來包覆要輸入的文字</li>
<li>常用的Character處理函數</li>
</ul>
<p>字串的剪接:<code>paste</code></p>
<pre class = 'prettyprint lang-r'>x <- "bubble"
y <- "bobble"
paste(x, y, sep=",")</pre>
<pre >[1] "bubble,bobble"</pre>
<p>字串的切割:<code>strsplit</code></p>
<pre class = 'prettyprint lang-r'>strsplit(x, "b")</pre>
<pre >[[1]]
[1] "" "u" "" "le"</pre>
<p>截取子字串:<code>substring</code></p>
<pre class = 'prettyprint lang-r'>name1<-"郭雪芙"
substring(name1, 1, 1)</pre>
<pre >[1] "郭"</pre>
<p>計算字串長度:<code>nchar</code></p>
<pre class = 'prettyprint lang-r'>nchar(name1) # 試著與 length 函數比較</pre>
<pre >[1] 3</pre>
</article></slide><slide class=''><hgroup><h2>小挑戰</h2></hgroup><article id="-2">
<ul>
<li>取出金城武的姓</li>
<li>取出字串 <code>a <- "2015-12-14"</code> 的月份</li>
</ul>
</article></slide><slide class=''><hgroup><h2>解答</h2></hgroup><article >
<pre class = 'prettyprint lang-r'># 取出金城武的
name2<-"金城武"
substring(name2, 1, 2)</pre>
<pre >[1] "金城"</pre>
<pre class = 'prettyprint lang-r'># 取出字串 "2015-12-14" 的月份
a <- "2015-12-14"
substring(a, 6, 7)</pre>
<pre >[1] "12"</pre>
<pre class = 'prettyprint lang-r'>tmp <- strsplit(a, "-")
tmp[[1]][2]</pre>
<pre >[1] "12"</pre>
</article></slide><slide class=''><hgroup><h2>Factor (類別)</h2></hgroup><article id="factor-" class="columns-2">
<p>如何處理名目變數?</p>
<pre class = 'prettyprint lang-r'>x <- c("F","M","F","F")
x</pre>
<pre >[1] "F" "M" "F" "F"</pre>
<pre class = 'prettyprint lang-r'>x <- factor(c("F","M","F","F"), levels=c("F","M"))
x</pre>
<pre >[1] F M F F
Levels: F M</pre>
<pre class = 'prettyprint lang-r'>x <- factor(c("F","M","F","F"), levels=c("F"))
levels(x)</pre>
<pre >[1] "F"</pre>
<pre class = 'prettyprint lang-r'>as.integer(x)</pre>
<pre >[1] 1 NA 1 1</pre>
</article></slide><slide class=''><hgroup><h2>如何處理順序資料?</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>#農業社會 男尊女卑
Argri <- factor(c("F","M","F","F"), order=TRUE, levels=c("F","M"))
#阿美族 女尊男卑
Amis <- factor(c("F","M","F","F"), order=FALSE, levels=c("F","M"))
#應該要用
Amis <- factor(c("F","M","F","F"), order=TRUE, levels=c("M","F"))</pre>
</article></slide><slide class=''><hgroup><h2>換個例子</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>#舉一個認真的例子 - 班上一號到六號分別拿到A, B, C的級別
rank=factor(c("C","A","B","B","C","C"), order=TRUE, level=c("C","B","A"))
rank</pre>
<pre >[1] C A B B C C
Levels: C < B < A</pre>
<pre class = 'prettyprint lang-r'>rank[1] < rank[2]</pre>
<pre >[1] TRUE</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch08: 資料型態的轉換</h2></hgroup><article id="ch08-">
</article></slide><slide class=''><hgroup><h2>向量有同質性 Vector</h2></hgroup><article id="-vector">
<p>Character > Numeric > Integer > Logical</p>
<pre class = 'prettyprint lang-r'>x <- c(1, 2.0, "3")
x</pre>
<pre >[1] "1" "2" "3"</pre>
</article></slide><slide class=''><hgroup><h2>資料型態的轉換</h2></hgroup><article class="columns-2">
<ul>
<li>利用以下函數自行轉換向量型態: <code>as.character</code>, <code>as.numeric</code>, <code>as.logical</code>。</li>
</ul>
<pre class = 'prettyprint lang-r'>as.numeric("2")</pre>
<pre >[1] 2</pre>
<pre class = 'prettyprint lang-r'>x <- c(1, 2.0, "3")
as.numeric(x)</pre>
<pre >[1] 1 2 3</pre>
<pre class = 'prettyprint lang-r'>y <- c("1", "2", "3", "2", "a")
as.numeric(y)</pre>
<pre >Warning: NAs introduced by coercion</pre>
<pre >[1] 1 2 3 2 NA</pre>
<ul>
<li><code>NA</code>代表Not available,代表著<strong>missing value</strong></li>
</ul>
</article></slide><slide class=''><hgroup><h2>資料型態的轉換2</h2></hgroup><article id="2" class="columns-2">
<pre class = 'prettyprint lang-r'># 字串轉數字
a1 <- c("89", "91", "102")
as.numeric(a1)</pre>
<pre >[1] 89 91 102</pre>
<pre class = 'prettyprint lang-r'># 布林轉數字
a2 <- c(TRUE, TRUE, FALSE)
as.numeric(a2)</pre>
<pre >[1] 1 1 0</pre>
<pre class = 'prettyprint lang-r'># 數字轉布林
a3 <- c(-2, -1, 0, 1, 2) # 只有0會被轉成FALSE
as.logical(a3)</pre>
<pre >[1] TRUE TRUE FALSE TRUE TRUE</pre>
<pre class = 'prettyprint lang-r'># 數字轉字串
as.character(a3)</pre>
<pre >[1] "-2" "-1" "0" "1" "2" </pre>
<pre class = 'prettyprint lang-r'>z <- c(1, 2, 3, 2, 3, 2, 1)
as.character(z) # 字串</pre>
<pre >[1] "1" "2" "3" "2" "3" "2" "1"</pre>
<pre class = 'prettyprint lang-r'>factor(z) # 轉類別</pre>
<pre >[1] 1 2 3 2 3 2 1
Levels: 1 2 3</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch09: List 存放異質性資料的容器</h2></hgroup><article id="ch09-list-">
</article></slide><slide class=''><hgroup><h2>List</h2></hgroup><article id="list">
<pre class = 'prettyprint lang-r'>x1 <- c("林志玲", 174, 52, TRUE) # 所有元素都被轉換成字串
x1</pre>
<pre >[1] "林志玲" "174" "52" "TRUE" </pre>
<pre class = 'prettyprint lang-r'>x2 <- list("林志玲", 174, 52, TRUE) # 保留資料型態
str(x2)</pre>
<pre >List of 4
$ : chr "林志玲"
$ : num 174
$ : num 52
$ : logi TRUE</pre>
</article></slide><slide class=''><hgroup><h2>List 賦值/取值</h2></hgroup><article id="list-" class="columns-2">
<pre class = 'prettyprint lang-r'>x3 <- list(name=c("林志玲", "隋棠", "蔡依林"),
height=c(174, 173, 158),
weight=c(52, 48, 39),
model=c(TRUE, TRUE, FALSE))
x3[[1]]</pre>
<pre >[1] "林志玲" "隋棠" "蔡依林"</pre>
<pre class = 'prettyprint lang-r'>x3$name</pre>
<pre >[1] "林志玲" "隋棠" "蔡依林"</pre>
<pre class = 'prettyprint lang-r'>x3[["name"]]</pre>
<pre >[1] "林志玲" "隋棠" "蔡依林"</pre>
<pre class = 'prettyprint lang-r'>names(x3)</pre>
<pre >[1] "name" "height" "weight" "model" </pre>
<pre class = 'prettyprint lang-r'># names(x3) <- c("Name", "Height", "Weight", "Model")</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Ch10:DataFrame 資料表</h2></hgroup><article id="ch10dataframe-">
</article></slide><slide class=''><hgroup><h2>資料表 <code>data.frame</code></h2></hgroup><article id="-data.frame">
<ul>
<li><code>data.frame</code> 是資料分析時最基本的物件</li>
<li><code>R</code> 提供將外部資料轉成 <code>data.frame</code> 的功能</li>
<li>透過<code>data.frame</code> 可以進行以下功能:
<ul>
<li>資料的整理</li>
<li>圖形的繪製</li>
<li>模型的配適與預測</li>
</ul></li>
</ul>
</article></slide><slide class=''><hgroup><h2>世界上最常見的範例資料 <code>iris</code></h2></hgroup><article id="-iris">
<pre class = 'prettyprint lang-r'>data("iris")
head(iris) # 列出前幾筆資料, 預設6筆</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa