-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRProgramming.html
More file actions
executable file
·1332 lines (1056 loc) · 49.3 KB
/
Copy pathRProgramming.html
File metadata and controls
executable file
·1332 lines (1056 loc) · 49.3 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>R Programming</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="viewport" content="width=device-width, initial-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="RProgramming_files/ioslides-13.5.1/fonts/fonts.css">
<link rel="stylesheet" media="all" href="RProgramming_files/ioslides-13.5.1/theme/css/default.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="RProgramming_files/ioslides-13.5.1/theme/css/phone.css">
<base target="_blank">
<script type="text/javascript">
var SLIDE_CONFIG = {
// Slide settings
settings: {
title: 'R Programming',
useBuilds: true,
usePrettify: true,
enableSlideAreas: true,
enableTouch: true,
},
// Author information
presenters: [
{
name: 'Johnson Hsieh (<a href="mailto:johnson@dsp.im">johnson@dsp.im</a>)' ,
company: '',
gplus: '',
twitter: '',
www: '',
github: ''
},
]
};
</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>
</hgroup>
</slide>
<slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch01:表格型文件的輸入與輸出</h2></hgroup><article id="ch01">
</article></slide><slide class=''><hgroup><h2>讀取表格檔案</h2></hgroup><article >
<ul>
<li>下載範例資料 <a href='http://johnsonhsieh.github.io/dsp-introR/data/hsb.csv' title=''>http://johnsonhsieh.github.io/dsp-introR/data/hsb.csv</a></li>
<li>利用<code>read.csv</code> 讀取 <code>csv</code> 檔 (一種以逗點分隔欄位的資料格式)</li>
</ul>
<pre class = 'prettyprint lang-r'># 可以使用R指令來下載
url <- "http://johnsonhsieh.github.io/dsp-introR/data/hsb.csv"
download.file(url, destfile = "hsb.csv") # 把檔案另存成hbs.csv
dat <- read.csv("hsb.csv")
head(dat)</pre>
<pre > id sex race ses schtyp prog read write math science socst
1 70 male White 1 public general 57 52 41 47 57
2 121 female White 2 public vocational 68 59 53 63 61
3 86 male White 3 public general 44 33 54 58 31
4 141 male White 3 public vocational 63 44 47 53 56
5 172 male White 2 public academic 47 52 57 53 61
6 113 male White 2 public academic 44 52 51 63 61</pre>
</article></slide><slide class=''><hgroup><h2>讀取表格檔案</h2></hgroup><article id="-1">
<p>讀取自己電腦裡的csv檔</p>
<pre class = 'prettyprint lang-r'># 方法1. 透過介面選擇檔案所在路徑
path <- file.choose()
dat <- read.csv(path)
# 方法2. 自行輸入檔案所在路徑
path <- "/Volumes/LEEF SURGE/LearnR/hsb.csv"
dat <- read.csv(path)
# 方法3. 自行輸入檔案相對路徑
dat <- read.csv("hsb.csv")</pre>
</article></slide><slide class=''><hgroup><h2>輸出表格檔案</h2></hgroup><article >
<p>利用<code>write.csv</code>將<code>data.frame</code>格式的R物件另存成csv檔</p>
<pre class = 'prettyprint lang-r'>write.csv(dat, "hsb_new.csv", row.names=FALSE, quote=FALSE)</pre>
</article></slide><slide class=''><hgroup><h2>淺談路徑設定</h2></hgroup><article >
<ul>
<li><code>getwd</code> 查詢目前所在的資料夾路徑 (絕對路徑)</li>
<li><code>setwd</code> 設定所在的資料夾路徑 <br> (Session -> Set Working Directory -> Choose Directory)</li>
<li>檔案 (檔案路徑 file directory) vs. 資料夾 (工作路徑 working directory)</li>
</ul>
<pre class = 'prettyprint lang-r'>getwd()</pre>
<pre >[1] "/Volumes/Transcend/DSP/DSP-training/R爬析(中研院社會所)"</pre>
<pre class = 'prettyprint lang-r'>dat <- read.csv("hsb.csv")</pre>
</article></slide><slide class=''><hgroup><h2>排解疑難 - 常見的讀取錯誤1</h2></hgroup><article id="---1">
<p>路徑錯誤</p>
<pre class = 'prettyprint lang-r'>path <- "wrong_file_path"
dat <- read.csv(file = path)</pre>
<pre>
Error in file(file, "rt") : 無法開啟連結
此外: Warning message:
In file(file, "rt") : 無法開啟檔案 'wrong_file_path' :No such file or directory
</pre>
<ul>
<li>絕對路徑 -> 確認檔案是否存在</li>
<li>相對路徑 -> 利用<code>getwd</code>了解R 當下的路徑位置</li>
</ul>
</article></slide><slide class=''><hgroup><h2>排解疑難 - 常見的讀取錯誤2</h2></hgroup><article id="---2">
<p>格式錯誤</p>
<pre class = 'prettyprint lang-r'>path <- "hsb.csv"
dat <- read.csv(file = path, header = TRUE, sep = "1")</pre>
<pre>
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
more columns than column names
</pre>
<ul>
<li>利用其他編輯器確認分隔符號</li>
<li>確認每列的資料的欄位是正確的</li>
<li>必要時,請用其他文件編輯器校正欲讀取的檔案</li>
</ul>
</article></slide><slide class=''><hgroup><h2>排解疑難 - 常見的讀取錯誤3</h2></hgroup><article id="---3">
<p>編碼錯誤</p>
<pre class = 'prettyprint lang-r'>url <- "http://johnsonhsieh.github.io/dsp-introR/data/dsp-gift-2013-big5/%E8%B2%B7%E8%B3%A3st_A_10109_10109.csv"</pre>
<pre class = 'prettyprint lang-r'>dat <- read.csv(url)</pre>
<pre class="code">
Error in make.names(col.names, unique = TRUE) :
無效的多位元組字串於 <b6>m<c2><ed><a5><ab><b0><cf>
</pre>
<ul>
<li>查詢檔案的編碼</li>
<li>常見的中文編碼有UTF-8和BIG-5</li>
</ul>
<pre class = 'prettyprint lang-r'># 利用`fileEncoding`參數選擇檔案編碼 - big5 / utf8
dat2 <- read.csv(url, fileEncoding = "big5")</pre>
</article></slide><slide class=''><hgroup><h2>讀取其他軟體資料集</h2></hgroup><article >
<ul>
<li>For SPSS and Stata datasets, use the <code>foreign</code> package</li>
<li>Cars: <a href='http://calcnet.mth.cmich.edu/org/spss/V16_materials/DataSets_v16/Cars.sav' title=''>Cars.sav</a></li>
<li>Milk Production: <a href='http://www.ats.ucla.edu/stat/stata/examples/chp/p004.dta' title=''>p004.dat</a></li>
</ul>
<pre class = 'prettyprint lang-r'># install.packages("foreign") # 安裝R套件 foreign
library(foreign) # 載入套件
cars <- read.spss("data/Cars.sav", to.data.frame = TRUE)
milk <- read.dta("data/p004.dta")
# head(cars)
# head(milk)</pre>
</article></slide><slide class=''><hgroup><h2>讀取其他軟體資料集</h2></hgroup><article id="-1">
<ul>
<li>For SAS datasets, use the <code>sas7bdat</code> package</li>
<li>airline: <a href='http://www.principlesofeconometrics.com/sas/airline.sas7bdat' title=''>airline.sas7bdat</a></li>
</ul>
<pre class = 'prettyprint lang-r'># install.packages("sas7bdat")
library(sas7bdat)
airline <- read.sas7bdat("data/airline.sas7bdat")
head(airline)</pre>
<pre > YEAR Y W R L K
1 1948 1.214 0.243 0.1454 1.415 0.612
2 1949 1.354 0.260 0.2181 1.384 0.559
3 1950 1.569 0.278 0.3157 1.388 0.573
4 1951 1.948 0.297 0.3940 1.550 0.564
5 1952 2.265 0.310 0.3559 1.802 0.574
6 1953 2.731 0.322 0.3593 1.926 0.711</pre>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch02:讀取網頁內容(Lite)</h2></hgroup><article id="ch02lite">
</article></slide><slide class=''><hgroup><h2>逐行輸入與輸出</h2></hgroup><article >
<ul>
<li><code>readLines</code>, <code>writeLines</code></li>
<li>是讀取網頁原始碼的好工具</li>
</ul>
<pre class = 'prettyprint lang-r'>output <- file("output.txt")
writeLines(as.character(1:12), con = output)
input <- readLines(output)
input</pre>
<pre > [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12"</pre>
</article></slide><slide class=''><hgroup><h2>練習</h2></hgroup><article >
<p>從<a href='http://s40.ptt01.cc/post_17476' title=''>網頁中</a>,找出藝人官方身高</p>
<pre class = 'prettyprint lang-r'>web_page <- readLines("http://s40.ptt01.cc/post_17476")
matches <- gregexpr("[\u4E00-\u9FA5]+官方身高:[0-9]+", web_page)
tmp <- regmatches(web_page, matches)
unlist(tmp)</pre>
<pre >[1] "陳喬恩官方身高:165" "宋慧喬官方身高:161" "伊能靜官方身高:161"
[4] "倪妮官方身高:170" "宋慧喬官方身高:161" "張梓琳官方身高:182"
[7] "周迅官方身高:160" "周迅官方身高:160" "全智賢官方身高:174"</pre>
<p>其中:</p>
<pre >[\u4E00-\u9FA5] 表示所有中文字符
[0-9] 含數字之字串
[a]+ 一或多個 a</pre>
</article></slide><slide class=''><hgroup><h2>進階練習</h2></hgroup><article >
<p>找出<a href='http://www.319papago.idv.tw/lifeinfo/chingshin/chingshin-02.html' title=''>清心福全</a>台北市南港店的地址</p>
<pre class = 'prettyprint lang-r'>web_page <- readLines("http://www.319papago.idv.tw/lifeinfo/chingshin/chingshin-02.html")
matches <- gregexpr("台北市南港區[\u4E00-\u9FA5|0-9|(|)]+", web_page)
tmp <- regmatches(web_page, matches)
unlist(tmp) # 把 list 轉成 vector</pre>
<pre >[1] "台北市南港區同德路54號" "台北市南港區南港路3段20號"
[3] "台北市南港區南港路一段154號"</pre>
</article></slide><slide class=''><hgroup><h2>小挑戰</h2></hgroup><article >
<ul>
<li>找出清心福全台北市門市的電話號碼</li>
<li>提示:<code>"02-[0-9]+"</code></li>
</ul>
</article></slide><slide class=''><hgroup><h2>小挑戰</h2></hgroup><article id="-1">
<ul>
<li>找出清心福全台北市門市的電話號碼</li>
<li>提示:<code>"02-[0-9]+"</code></li>
</ul>
<h3>參考解答</h3>
<pre class = 'prettyprint lang-r'>web_page <- readLines("http://www.319papago.idv.tw/lifeinfo/chingshin/chingshin-02.html")
matches <- gregexpr("02-[0-9]+", web_page)
tmp <- regmatches(web_page, matches)
unlist(tmp) </pre>
<pre > [1] "02-28761717" "02-28311515" "02-28805757" "02-28829191" "02-28126988"
[6] "02-28835757" "02-28335151" "02-28382099" "02-25993301" "02-25964768"
[11] "02-25582856" "02-23256696" "02-27772151" "02-27041995" "02-87327588"
[16] "02-27780058" "02-23568851" "02-27096711" "02-87710602" "02-23661900"
[21] "02-27336155" "02-25335922" "02-25330098" "02-25412121" "02-25095253"
[26] "02-25362808" "02-25423939" "02-25007119" "02-27733737" "02-25042858"
[31] "02-25093117" "02-25212012" "02-25991177" "02-33225069" "02-23582277"
[36] "02-23753889" "02-23710880" "02-23678838" "02-27940591" "02-87971968"
[41] "02-87523300" "02-27996737" "02-87974399" "02-26306626" "02-87923883"
[46] "02-27903186" "02-29366236" "02-89351381" "02-29342121" "02-29305939"
[51] "02-25860202" "02-28910379" "02-28917784" "02-28278806" "02-28978088"
[56] "02-28286161" "02-28223996" "02-25288288" "02-27531966" "02-25775296"
[61] "02-27172228" "02-27629786" "02-27611525" "02-27475628" "02-23772478"
[66] "02-27677158" "02-27593118" "02-27587181" "02-27585123" "02-26533886"
[71] "02-27882811" "02-26532371" "02-23091959" "02-23143280" "02-23111818"
[76] "02-23024378" "02-23016677" "02-23026069" "02-23328618"</pre>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch03: 條件判斷語句 - if</h2></hgroup><article id="ch03----if">
</article></slide><slide class=''><hgroup><h2>條件判斷</h2></hgroup><article >
<ul>
<li>一般而言,R語言的程式碼是由上自下依序執行,</li>
<li>有時需要根據某些條件判斷執行某些分支</li>
<li>在R中使用<code>if</code>來進行判斷</li>
<li>將敘述內容寫在大括號<code>{ }</code>裡面</li>
</ul>
<pre class = 'prettyprint lang-r'># 判斷x是否大於2, 如果是,則印出結果
x <- 4
if(x > 2){
cat(x, "is larger than 2")
}</pre>
<pre >4 is larger than 2</pre>
</article></slide><slide class=''><hgroup><h2>條件判斷</h2></hgroup><article id="-1">
<ul>
<li>在R中使用<code>if</code>來進行判斷</li>
<li>將敘述內容寫在大括號<code>{ }</code>裡面</li>
<li>當敘述句只有一行時,可以省略大括號</li>
</ul>
<pre class = 'prettyprint lang-r'># 判斷x是否大於2, 如果是,則印出結果
x <- 4
if(x > 2) cat(x, "is larger than 2")</pre>
<pre >4 is larger than 2</pre>
</article></slide><slide class=''><hgroup><h2>條件判斷</h2></hgroup><article id="-2">
<ul>
<li>若需要兩個判斷分支時,加入<code>else</code></li>
</ul>
<pre class = 'prettyprint lang-r'>y <- 1
if(y > 2){
cat(y, "is larger than 2")
}else{
cat(y, "is smaller than 2")
} </pre>
<pre >1 is smaller than 2</pre>
</article></slide><slide class=''><hgroup><h2>條件判斷</h2></hgroup><article id="-3">
<ul>
<li>若需要兩個判斷分支時,加入<code>else</code></li>
<li>可以使用<code>ifelse</code>函數進行簡化,而且該函數支援向量化運算 (後述)</li>
</ul>
<pre class = 'prettyprint lang-r'>y <- 1
ifelse(y > 2, "larger than 2", "smaller than 2")</pre>
<pre >[1] "smaller than 2"</pre>
<pre class = 'prettyprint lang-r'>z <- 1:5
ifelse(z > 2, "larger than 2", "smaller than 2")</pre>
<pre >[1] "smaller than 2" "smaller than 2" "larger than 2" "larger than 2"
[5] "larger than 2" </pre>
</article></slide><slide class=''><hgroup><h2>條件判斷</h2></hgroup><article id="-4">
<ul>
<li>若需要多個判斷分支時,加入<code>else if</code>, …, <code>else</code></li>
</ul>
<pre class = 'prettyprint lang-r'>y <- 1
if(y > 2){
cat(y, "is larger than 2")
}else if(y < 2){
cat(y, "is smaller than 2")
}else{
cat(y, "is equal to 2")
}</pre>
<pre >1 is smaller than 2</pre>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch04: 自訂函數 - function</h2></hgroup><article id="ch04----function">
</article></slide><slide class=''><hgroup><h2>自訂函數 (<code>function</code>)</h2></hgroup><article id="-function">
<ul>
<li>函數是R語言的基石,有利重複使用</li>
<li>通過多個函數的組裝,可以完成更複雜的任務</li>
<li>函數在子環境中執行,不對外產生影響</li>
</ul>
<pre class = 'prettyprint lang-r'>foo <- function(x){
if(x > 2){
cat(x, "is larger than 2")
}else if(x < 2){
cat(x, "is smaller than 2")
}else{
cat(x, "is equal to 2")
}
}
foo(1); foo(2); foo(3)</pre>
<pre >1 is smaller than 2</pre>
<pre >2 is equal to 2</pre>
<pre >3 is larger than 2</pre>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch05: 循環語句 - for</h2></hgroup><article id="ch05----for">
</article></slide><slide class=''><hgroup><h2>循環語句</h2></hgroup><article >
<ul>
<li>可用來重複執行某段程式碼</li>
<li><code>for loop</code>中,已足標作為終止循環的條件</li>
</ul>
<pre class = 'prettyprint lang-r'>x <- 0
for(i in 1:100){
if(i %% 2 !=0) { # x是否為奇數
x <- x + i # 自身累加
}
}
x</pre>
<pre >[1] 2500</pre>
</article></slide><slide class=''><hgroup><h2>雙重循環語句</h2></hgroup><article >
<ul>
<li>可用來重複執行某段程式碼</li>
<li><code>\t</code> = tab鍵, <code>\n</code> = 空行</li>
</ul>
<pre class = 'prettyprint lang-r'>x <- 0
k <- 0
for(i in 1:4){
for(j in 1:3){
cat(paste(i, "*", j, "=", i*j, ",\t", sep=""))
k <- k + 1
x[k] <- i*j
}
cat("\n")
}</pre>
<pre >1*1=1, 1*2=2, 1*3=3,
2*1=2, 2*2=4, 2*3=6,
3*1=3, 3*2=6, 3*3=9,
4*1=4, 4*2=8, 4*3=12, </pre>
</article></slide><slide class=''><hgroup><h2>進階練習</h2></hgroup><article id="-1">
<ul>
<li>請先下載 <a href='https://github.com/JohnsonHsieh/R-basic-for-social-science/raw/gh-pages/data/movies.zip' title=''>movies.zip</a> (解壓縮後有21個檔案)</li>
<li>一口氣讀入全部的檔案 (Hint: <code>dir + for loop + list</code>)</li>
<li>將檔案合併 (Hint: <code>do.call + rbind</code>)</li>
</ul>
</article></slide><slide class=''><hgroup><h2>進階練習(1)</h2></hgroup><article id="1">
<pre class = 'prettyprint lang-r'># 展示movies資料夾下所有的檔案
fname <- dir("data/movies/")
fname</pre>
<pre > [1] "movies-1995.csv" "movies-1996.csv" "movies-1997.csv"
[4] "movies-1998.csv" "movies-1999.csv" "movies-2000.csv"
[7] "movies-2001.csv" "movies-2002.csv" "movies-2003.csv"
[10] "movies-2004.csv" "movies-2005.csv" "movies-2006.csv"
[13] "movies-2007.csv" "movies-2008.csv" "movies-2009.csv"
[16] "movies-2010.csv" "movies-2011.csv" "movies-2012.csv"
[19] "movies-2013.csv" "movies-2014.csv" "movies-2015.csv"</pre>
</article></slide><slide class=''><hgroup><h2>進階練習(2)</h2></hgroup><article id="2">
<pre class = 'prettyprint lang-r'># 展示movies資料夾下所有的檔案
fname <- dir("data/movies/")
# 練習讀一個csv檔 (movies-1995.csv)
path <- paste("data/movies/", fname[1], sep="")
mov95 <- read.csv(path)
head(mov95)</pre>
<pre > movieId title year
1 1 Toy Story 1995
2 2 Jumanji 1995
3 3 Grumpier Old Men 1995
4 4 Waiting to Exhale 1995
5 5 Father of the Bride Part II 1995
6 6 Heat 1995</pre>
</article></slide><slide class=''><hgroup><h2>進階練習(3)</h2></hgroup><article id="3">
<pre class = 'prettyprint lang-r'># 展示movies資料夾下所有的檔案
fname <- dir("data/movies/")
# 練習讀多個csv檔
mov <- list() # 預設一個名為 mov 的list容器
for(i in 1:length(fname)){
path <- paste("data/movies/", fname[i], sep="")
mov[[i]] <- read.csv(path)
}
head(mov[[1]])</pre>
<pre > movieId title year
1 1 Toy Story 1995
2 2 Jumanji 1995
3 3 Grumpier Old Men 1995
4 4 Waiting to Exhale 1995
5 5 Father of the Bride Part II 1995
6 6 Heat 1995</pre>
</article></slide><slide class=''><hgroup><h2>進階練習(4)</h2></hgroup><article id="4">
<pre class = 'prettyprint lang-r'># 將list用列合併(rbind)整合成data.frame
df <- do.call(rbind, mov)
head(df)</pre>
<pre > movieId title year
1 1 Toy Story 1995
2 2 Jumanji 1995
3 3 Grumpier Old Men 1995
4 4 Waiting to Exhale 1995
5 5 Father of the Bride Part II 1995
6 6 Heat 1995</pre>
</article></slide><slide class=''><hgroup><h2>小挑戰</h2></hgroup><article id="-2">
<ul>
<li>只讀取1995-1999電影資料</li>
<li>提示:<code>gregexpr("movies-199[5-9]+.csv", fname)</code></li>
</ul>
</article></slide><slide class=''><hgroup><h2>小挑戰 (參考解答)</h2></hgroup><article id="-">
<ul>
<li>只讀取1995-1999電影資料</li>
<li>提示:<code>gregexpr("movies-199[5-9]+.csv", fname)</code></li>
</ul>
<pre class = 'prettyprint lang-r'>matches <- gregexpr("movies-199[5-9]+.csv", fname)
tmp <- regmatches(fname, matches)
fname90 <- unlist(tmp)
mov90 <- list() # 預設一個名為 mov 的list容器
for(i in 1:length(fname90)){
path <- paste("data/movies/", fname90[i], sep="")
mov90[[i]] <- read.csv(path)
}
df90 <- do.call(rbind, mov90)
# tail(df90) # 最後6筆</pre>
</article></slide><slide class=''><hgroup><h2>練習寫一個批次讀檔函數</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>myRead <- function(fname, path0="data/movies/"){
mov <- list()
for(i in 1:length(fname)){
path <- paste(path0, fname[i], sep="")
mov[[i]] <- read.csv(path)
}
do.call(rbind, mov)
}
df <- myRead(fname90, path0="data/movies/")
head(df)</pre>
<pre > movieId title year
1 1 Toy Story 1995
2 2 Jumanji 1995
3 3 Grumpier Old Men 1995
4 4 Waiting to Exhale 1995
5 5 Father of the Bride Part II 1995
6 6 Heat 1995</pre>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>Ch06: Pipe Line Coding Style</h2></hgroup><article id="ch06-pipe-line-coding-style">
</article></slide><slide class=''><hgroup><h2>2014 年最有影響的套件之一:magrittr</h2></hgroup><article id="magrittr">
<ul>
<li>壓縮的程式碼不好讀</li>
<li>展開的程式碼會產生很多暫存變數</li>
<li>套件<code>magrittr</code>部份解決了這個問題</li>
<li>基本算子 %>%, %<>%, %T>%, %$%</li>
</ul>
</article></slide><slide class=''><hgroup><h2>基本算子 (<code>%>%</code>)</h2></hgroup><article >
<ul>
<li>想像一下程式的寫作與閱讀邏輯</li>
<li><code>%>%</code> 會將算子左邊的物件 (object) 傳到右邊的函數 (function) 中第一個argument</li>
<li>. 點號適合用在欲傳入變數不是在傳入函數的第一個位置時使用</li>
<li>use <code>x %>% f</code>, rather than <code>f(x)</code></li>
<li>or use <code>x %>% f(y, z)</code>, rather than <code>f(x, y, z)</code></li>
<li>or <code>y %>% f(x, ., z)</code>, rather than <code>f(x, y, z)</code></li>
</ul>
<pre class = 'prettyprint lang-r'># install.packages("magrittr")
library(magrittr)
x <- 1:10
mean(x)</pre>
<pre >[1] 5.5</pre>
<pre class = 'prettyprint lang-r'>x %>% mean # 由左而右順序操作</pre>
<pre >[1] 5.5</pre>
</article></slide><slide class=''><hgroup><h2>幾種等價用法</h2></hgroup><article >
<p>利用三角形面積公式說明%>%算子的幾種等價用法</p>
<pre class = 'prettyprint lang-r'>tri_area <- function(a, h=5) a*h/2
a <- 10
tri_area(a)</pre>
<pre >[1] 25</pre>
<pre class = 'prettyprint lang-r'>a %>% tri_area # 省略括號</pre>
<pre >[1] 25</pre>
<pre class = 'prettyprint lang-r'>a %>% tri_area(h=5) # 保留括號</pre>
<pre >[1] 25</pre>
<pre class = 'prettyprint lang-r'>a %>% tri_area(., h=5) # 以 `.` 來表示欲傳入的變數</pre>
<pre >[1] 25</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article id="section">
<pre class = 'prettyprint lang-r'># 指令壓縮
plot(density(sample(mtcars$mpg, size=10000, replace=TRUE), kernel="gaussian"), col="red", main="density of mpg")</pre>
<pre class = 'prettyprint lang-r'># Pipe Line
mtcars$mpg %>%
sample(size=10000, replace=TRUE) %>%
density(kernel="gaussian") %>%
plot(col="red", main="density of mpg")</pre>
<p><img src="RProgramming_files/figure-html/unnamed-chunk-34-1.png" title="" alt="" width="720" /></p>
</article></slide><slide class='segue dark nobackground'><hgroup class = 'auto-fadein'><h2>資料處理</h2></hgroup><article >
</article></slide><slide class=''><hgroup><h2>資料拉皮 <code>reshape2</code></h2></hgroup><article id="-reshape2">
<ul>
<li><code>melt</code>: wide format -> long format</li>
<li><code>dcast</code>: long format -> wide format
<center>
<img src='img/reshaping.png' height=400 align='center'>
</center></li>
</ul>
</article></slide><slide class=''><hgroup><h2>資料拉皮 <code>reshape2</code></h2></hgroup><article id="-reshape2-1">
<ul>
<li><code>melt</code>: wide format -> long format</li>
<li><code>dcast</code>: long format -> wide format</li>
</ul>
<pre class = 'prettyprint lang-r'># install.packages("reshape2")
library(reshape2)
WP.melt <- data.frame(WorldPhones)
WP.melt$year <- rownames(WP.melt)
WP.melt <- melt(WP.melt, id="year")
head(WP.melt)</pre>
<pre > year variable value
1 1951 N.Amer 45939
2 1956 N.Amer 60423
3 1957 N.Amer 64721
4 1958 N.Amer 68484
5 1959 N.Amer 71799
6 1960 N.Amer 76036</pre>
</article></slide><slide class=''><hgroup><h2>資料拉皮 <code>reshape2</code></h2></hgroup><article id="-reshape2-2">
<ul>
<li><code>melt</code>: wide format -> long format</li>
<li><code>dcast</code>: long format -> wide format</li>
</ul>
<pre class = 'prettyprint lang-r'>WP.cast <- dcast(WP.melt, year~variable,value.var="value")
head(WP.cast)</pre>
<pre > year N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
1 1951 45939 21574 2876 1815 1646 89 555
2 1956 60423 29990 4708 2568 2366 1411 733
3 1957 64721 32510 5230 2695 2526 1546 773
4 1958 68484 35218 6662 2845 2691 1663 836
5 1959 71799 37598 6856 3000 2868 1769 911
6 1960 76036 40341 8220 3145 3054 1905 1008</pre>
</article></slide><slide class=''><hgroup><h2>2014 年最有影響的套件之一:dplyr</h2></hgroup><article id="dplyr">
<ul>
<li>讓R 使用者可以用更有彈性的方式來處理資料</li>
<li>針對<code>data.frame</code>做設計(名稱中的<code>d</code>)</li>
<li>設計理念
<ul>
<li>導入資料整理最重要的動作(非常類似SQL)</li>
<li>快</li>
<li>支援異質資料源(<code>data.frame</code>或資料庫中的表格)</li>
</ul></li>
</ul>
</article></slide><slide class=''><hgroup><h2>學習dplyr的官方方式:<code>vignette</code></h2></hgroup><article id="dplyrvignette">
<pre class = 'prettyprint lang-r'>vignette(all = TRUE, package = "dplyr")
vignette("introduction", package = "dplyr")</pre>
<ul>
<li>更詳細的dplyr介紹可以閱讀dplyr的小論文</li>
<li>R 的開發者會針對一個主題撰寫小論文做介紹</li>
</ul>
</article></slide><slide class=''><hgroup><h2>dplyr簡介</h2></hgroup><article id="dplyr" class="columns-2">
<ul>
<li><code>arrange</code> 排列</li>
<li><code>filter</code> 對列做篩選 (row)</li>
<li><code>select</code> 對欄做篩選 (column)</li>
<li><code>mutate</code> 更改欄或新增欄</li>
<li>**<code>group_by</code> + <code>summarise</code> 分類</li>
</ul>
<center>
<img src='img/R_ETL_Fn1.png' style='max-width: 100%;max-height: 100%'></img>
</center>
<p>出處:<a href='https://www.youtube.com/watch?v=JD1eDxxrur0' title=''>資料科學愛好者年會資料分析上手課程:ETL1</a></p>
</article></slide><slide class=''><hgroup><h2>資料排序 <code>arrange</code></h2></hgroup><article id="-arrange">
<center>
<img src='img/arrange.png' height=300 align='center'></img><img src='img/arranged.png' height=300 align='center'></img>
</center>
</article></slide><slide class=''><hgroup><h2>資料排序</h2></hgroup><article >
<ul>
<li>arrange(dataframe, 要進行排序的項目,….)</li>
<li>排序預設是由小到大</li>
</ul>
<pre class = 'prettyprint lang-r'>arrange(iris, Petal.Length) %>% head</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 4.6 3.6 1.0 0.2 setosa
2 4.3 3.0 1.1 0.1 setosa
3 5.8 4.0 1.2 0.2 setosa
4 5.0 3.2 1.2 0.2 setosa
5 4.7 3.2 1.3 0.2 setosa
6 5.4 3.9 1.3 0.4 setosa</pre>
</article></slide><slide class=''><hgroup><h2>資料排序</h2></hgroup><article id="-1">
<ul>
<li>arrange(dataframe, 要進行排序的項目,….)</li>
<li>排序預設是由小到大,加上<code>desc</code>可使用遞增排列</li>
</ul>
<pre class = 'prettyprint lang-r'>arrange(iris, desc(Petal.Length)) %>% head</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.7 2.6 6.9 2.3 virginica
2 7.7 3.8 6.7 2.2 virginica
3 7.7 2.8 6.7 2.0 virginica
4 7.6 3.0 6.6 2.1 virginica
5 7.9 3.8 6.4 2.0 virginica
6 7.3 2.9 6.3 1.8 virginica</pre>
</article></slide><slide class=''><hgroup><h2>資料排序</h2></hgroup><article id="-2">
<ul>
<li>arrange(dataframe, 要進行排序的項目,….)</li>
<li>排序預設是由小到大,加上<code>desc</code>可使用遞增排列</li>
<li>排序的項目可以不只一項,只要依序將項目填入後方即可</li>
</ul>
<pre class = 'prettyprint lang-r'>arrange(iris, desc(Petal.Length), - Sepal.Length ) %>% head</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.7 2.6 6.9 2.3 virginica
2 7.7 3.8 6.7 2.2 virginica
3 7.7 2.8 6.7 2.0 virginica
4 7.6 3.0 6.6 2.1 virginica
5 7.9 3.8 6.4 2.0 virginica
6 7.3 2.9 6.3 1.8 virginica</pre>
</article></slide><slide class=''><hgroup><h2>dplyr 常用函數</h2></hgroup><article id="dplyr-">
<ul>
<li>移除重複資料: <code>distinct(iris)</code></li>
<li>隨機抽取資料: <code>sample_n(iris, 5)</code></li>
<li>抽取指定列: <code>slice(iris, c(1,3,4,5))</code></li>
</ul>
</article></slide><slide class=''><hgroup><h2>對列做篩選 <code>filter</code></h2></hgroup><article id="-filter">
<center>
<img src='img/filter1.png' width=300 align='center'></img><img src='img/filter2.png' width=300 align='center'>
</center>
</article></slide><slide class=''><hgroup><h2>對列做篩選 <code>filter</code></h2></hgroup><article id="-filter-1">
<pre class = 'prettyprint lang-r'>filter(iris,Sepal.Length == 7.7)</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.7 3.8 6.7 2.2 virginica
2 7.7 2.6 6.9 2.3 virginica
3 7.7 2.8 6.7 2.0 virginica
4 7.7 3.0 6.1 2.3 virginica</pre>
<pre class = 'prettyprint lang-r'>filter(iris,Sepal.Length == 7.7,Sepal.Width ==2.8)</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.7 2.8 6.7 2 virginica</pre>
</article></slide><slide class=''><hgroup><h2>對行做選取 <code>select</code></h2></hgroup><article id="-select">
<center>
<img src='img/select1.png' height=300 align='center'></img><img src='img/select2.png' height=300 align='center'>
</center>
</article></slide><slide class=''><hgroup><h2>對行做選取 <code>select</code></h2></hgroup><article id="-select-1">
<pre class = 'prettyprint lang-r'>select(iris,c(Petal.Width, Species))</pre>
<pre > Petal.Width Species
1 0.2 setosa
2 0.2 setosa
3 0.2 setosa
4 0.2 setosa
5 0.2 setosa
6 0.4 setosa
7 0.3 setosa
8 0.2 setosa
9 0.2 setosa
10 0.1 setosa
11 0.2 setosa
12 0.2 setosa
13 0.1 setosa
14 0.1 setosa
15 0.2 setosa
16 0.4 setosa
17 0.4 setosa
18 0.3 setosa
19 0.3 setosa
20 0.3 setosa
21 0.2 setosa
22 0.4 setosa
23 0.2 setosa
24 0.5 setosa
25 0.2 setosa
26 0.2 setosa
27 0.4 setosa
28 0.2 setosa
29 0.2 setosa
30 0.2 setosa
31 0.2 setosa
32 0.4 setosa
33 0.1 setosa
34 0.2 setosa
35 0.2 setosa
36 0.2 setosa
37 0.2 setosa
38 0.1 setosa
39 0.2 setosa
40 0.2 setosa
41 0.3 setosa
42 0.3 setosa
43 0.2 setosa
44 0.6 setosa
45 0.4 setosa
46 0.3 setosa
47 0.2 setosa
48 0.2 setosa
49 0.2 setosa
50 0.2 setosa
51 1.4 versicolor
52 1.5 versicolor
53 1.5 versicolor
54 1.3 versicolor
55 1.5 versicolor
56 1.3 versicolor
57 1.6 versicolor
58 1.0 versicolor
59 1.3 versicolor
60 1.4 versicolor
61 1.0 versicolor
62 1.5 versicolor
63 1.0 versicolor
64 1.4 versicolor
65 1.3 versicolor
66 1.4 versicolor
67 1.5 versicolor
68 1.0 versicolor
69 1.5 versicolor
70 1.1 versicolor
71 1.8 versicolor
72 1.3 versicolor
73 1.5 versicolor
74 1.2 versicolor
75 1.3 versicolor
76 1.4 versicolor
77 1.4 versicolor
78 1.7 versicolor
79 1.5 versicolor
80 1.0 versicolor
81 1.1 versicolor
82 1.0 versicolor
83 1.2 versicolor
84 1.6 versicolor
85 1.5 versicolor
86 1.6 versicolor
87 1.5 versicolor
88 1.3 versicolor
89 1.3 versicolor
90 1.3 versicolor
91 1.2 versicolor
92 1.4 versicolor
93 1.2 versicolor
94 1.0 versicolor
95 1.3 versicolor
96 1.2 versicolor
97 1.3 versicolor
98 1.3 versicolor
99 1.1 versicolor
100 1.3 versicolor
101 2.5 virginica
102 1.9 virginica
103 2.1 virginica
104 1.8 virginica
105 2.2 virginica
106 2.1 virginica
107 1.7 virginica
108 1.8 virginica
109 1.8 virginica
110 2.5 virginica
111 2.0 virginica
112 1.9 virginica
113 2.1 virginica
114 2.0 virginica
115 2.4 virginica
116 2.3 virginica
117 1.8 virginica
118 2.2 virginica
119 2.3 virginica
120 1.5 virginica
121 2.3 virginica
122 2.0 virginica
123 2.0 virginica
124 1.8 virginica
125 2.1 virginica
126 1.8 virginica
127 1.8 virginica
128 1.8 virginica
129 2.1 virginica
130 1.6 virginica
131 1.9 virginica