-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataETL.html
More file actions
executable file
·1488 lines (1051 loc) · 63.4 KB
/
Copy pathDataETL.html
File metadata and controls
executable file
·1488 lines (1051 loc) · 63.4 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>Data ETL with R</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-06-06" />
<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: 'Data ETL with R',
subtitle: 'Data Extract, Transform and Load',
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="DataETL_files/ioslides-13.5.1/fonts/fonts.css" rel="stylesheet" />
<link href="DataETL_files/ioslides-13.5.1/theme/css/default.css" rel="stylesheet" />
<link href="DataETL_files/ioslides-13.5.1/theme/css/phone.css" rel="stylesheet" />
<script src="DataETL_files/ioslides-13.5.1/js/modernizr.custom.45394.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/prettify/prettify.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/prettify/lang-r.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/prettify/lang-yaml.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/hammer.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/slide-controller.js"></script>
<script src="DataETL_files/ioslides-13.5.1/js/slide-deck.js"></script>
<script src="DataETL_files/htmlwidgets-0.9/htmlwidgets.js"></script>
<script src="DataETL_files/jquery-1.12.4/jquery.min.js"></script>
<script src="DataETL_files/datatables-binding-0.2/datatables.js"></script>
<link href="DataETL_files/dt-core-1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="DataETL_files/dt-core-1.10.12/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="DataETL_files/dt-core-1.10.12/js/jquery.dataTables.min.js"></script>
<link href="DataETL_files/dt-ext-fixedcolumns-1.10.12/css/fixedColumns.dataTables.min.css" rel="stylesheet" />
<script src="DataETL_files/dt-ext-fixedcolumns-1.10.12/js/dataTables.fixedColumns.min.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-06-06</p>
</hgroup>
</slide>
<slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data ETL 課程大綱</h2></hgroup><article id="data-etl-">
</article></slide><slide class=''><hgroup><h2>Agenda</h2></hgroup><article id="agenda">
<ul>
<li>Data Input / Output:<strong>學會</strong> 讀取不同種的資料格式與輸出。</li>
<li>Data Manipulation:<strong>學會</strong> 如何將資料操之在手。</li>
<li>Data Aggregation: <strong>學會</strong> 如何彙總出你有興趣的數字。</li>
</ul>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data I/O:表格型文件的輸入與輸出</h2></hgroup><article id="data-io">
</article></slide><slide class=''><hgroup><h2>在讀檔案之前,先了解路徑的種類</h2></hgroup><article >
<p>路徑分為兩種:</p>
<ul>
<li>絕對路徑:一般大家所認知的路徑長相。</li>
<li>相對路徑:從 <code>working directory(工作目錄) 開始尋找</code></li>
</ul>
<pre class = 'prettyprint lang-r'>## 查看目前 Working directory 在哪個位置
getwd()
## 查看該工作目錄底下有什麼檔案
dir()
## 更改、設定工作目錄位置
# setwd('這邊放路徑')</pre>
</article></slide><slide class=''><hgroup><h2>輸入表格檔案(1/3)</h2></hgroup><article id="13">
<ul>
<li><a href='https://raw.githubusercontent.com/unityculture/PIXNET_workshop/master/data/transaction.csv' title=''>下載範例資料</a></li>
<li>利用<code>read.csv</code> 讀取 <code>csv</code> 檔 (一種以逗點分隔欄位的資料格式)<br/></li>
<li>路徑必須指到下載的位置</li>
</ul>
<pre class = 'prettyprint lang-r'>############### 絕對路徑 ###############
# 請輸入完整的檔案路徑
transaction <- read.csv("/Users/sheng/Desktop/data/transaction.csv") #如果你是mac
transaction <- read.csv("C:\\Users\\transaction.csv") #如果你是windows
############### 相對路徑 ###############
# 設定我們檔案存放的路徑
setwd()
# 讀檔起手式
transaction <- read.csv("transaction.csv")
# 若讀入的是亂碼,猜猜看以下兩種編碼 utf-8 and big5
transaction <- read.csv("transaction.csv",fileEncoding = 'big5')
transaction <- read.csv("transaction.csv",fileEncoding = 'utf-8')</pre>
</article></slide><slide class=''><hgroup><h2>輸入表格檔案(2/3)</h2></hgroup><article id="23">
<p>看看資料輸入後的結果有沒有問題:</p>
<pre class = 'prettyprint lang-r'>transaction <- read.csv("data/transaction.csv")
head(transaction)</pre>
<center>
<img src = 'img/head_data.jpg' width = 85%>
</center>
<p>. . .</p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<div style="float:left;width:48%;">
<table class = 'rmdtable'>
<tr class="header">
<th align="left">英文欄位名稱</th>
<th align="left">中文欄位名稱</th>
</tr>
<tr class="odd">
<td align="left">city</td>
<td align="left">縣市</td>
</tr>
<tr class="even">
<td align="left">district</td>
<td align="left">鄉鎮市區</td>
</tr>
<tr class="odd">
<td align="left">trac_year</td>
<td align="left">交易年份</td>
</tr>
<tr class="even">
<td align="left">trac_month</td>
<td align="left">交易月份</td>
</tr>
<tr class="odd">
<td align="left">trac_type</td>
<td align="left">交易標的</td>
</tr>
<tr class="even">
<td align="left">trac_content</td>
<td align="left">交易筆棟數</td>
</tr>
<tr class="odd">
<td align="left">use_type</td>
<td align="left">使用分區或編定</td>
</tr>
</table></div>
<div style="float:right;width:50%;">
<table class = 'rmdtable'>
<tr class="header">
<th align="left">英文欄位名稱</th>
<th align="left">中文欄位名稱</th>
</tr>
<tr class="odd">
<td align="left">build_type</td>
<td align="left">建物型態</td>
</tr>
<tr class="even">
<td align="left">build_ymd</td>
<td align="left">建築完成年月</td>
</tr>
<tr class="odd">
<td align="left">area_land</td>
<td align="left">土地移轉總面積.平方公尺.</td>
</tr>
<tr class="even">
<td align="left">area_build</td>
<td align="left">建物移轉總面積.平方公尺.</td>
</tr>
<tr class="odd">
<td align="left">area_park</td>
<td align="left">車位移轉總面積.平方公尺.</td>
</tr>
<tr class="even">
<td align="left">price_total</td>
<td align="left">總價.元.</td>
</tr>
<tr class="odd">
<td align="left">price_unit</td>
<td align="left">單價.元.平方公尺.</td>
</tr>
</table></div>
</article></slide><slide class=''><hgroup><h2>輸入表格檔案(3/3)</h2></hgroup><article id="33">
<p>若是資料不如 <code>csv</code> 檔以 <code>,</code> 做分隔呢?</p>
<p><code>read.table()</code>可以解決上述的問題,透過<code>?read.table()</code>可以查看其中可以調整的參數。注意到<code>sep =</code>是指定要輸入的資料是用什麼符號做分隔,當 <code>read.table()</code> 的 <code>sep = ','</code>時,跟 <code>read.csv()</code> 是相同的。</p>
<pre class = 'prettyprint lang-r'>transaction <- read.csv("data/transaction.csv")
transaction <- read.table("data/transaction.csv", sep = ',', header = T)</pre>
</article></slide><slide class=''><hgroup><h2>讀取JSON檔案 (1/2)</h2></hgroup><article id="json-12">
<pre class = 'prettyprint lang-r'>install.packages("jsonlite")</pre>
<center>
<img src = 'img/csvtojson.jpg' height = 400>
</center>
</article></slide><slide class=''><hgroup><h2>讀取JSON檔案 (2/2)</h2></hgroup><article id="json-22">
<ul>
<li><code>fromJSON</code>: 將 <code>JSON</code> 轉換成 Factor, DataFrame, Matrix 等格式,<code>?fromJSON</code> 查詢。</li>
<li><code>toJSON</code>: <code>?toJSON</code></li>
</ul>
<pre class = 'prettyprint lang-r'>json <-
'[
{"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"},
{"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}
]'
mydf <- fromJSON(json)
mydf</pre>
<pre > Name Age Occupation
1 Mario 32 Plumber
2 Peach 21 Princess</pre>
</article></slide><slide class=''><hgroup><h2>輸出表格檔案</h2></hgroup><article >
<p>利用<code>write.csv</code>將<code>data.frame</code>格式的R物件另存成csv檔。為了效率,我們僅將 <code>head(data)</code> 6筆資料做輸出成 <code>transaction_head.csv</code> 即可。</p>
<pre class = 'prettyprint lang-r'>write.csv(head(transaction), "transaction_head.csv", row.names=FALSE, quote=FALSE)</pre>
<center>
<img src = 'img/writecsv.jpg' width = 60%>
</center>
</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 <- "data/transaction.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>利用<strong>其他編輯器</strong>確認分隔符號</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>常見的中文編碼有<strong>UTF-8</strong>和<strong>BIG-5</strong></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='https://stats.idre.ucla.edu/stat/stata/examples/chp/p004.dta' title=''>p004.dta</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")</pre>
<pre >Warning in strptime(x, format, tz = tz): unknown timezone 'zone/tz/2018c.
1.0/zoneinfo/Asia/Taipei'</pre>
<pre class = 'prettyprint lang-r'>head(airline)</pre>
<pre > YEAR Y W R L K
1 1948 1.2 0.24 0.15 1.4 0.61
2 1949 1.4 0.26 0.22 1.4 0.56
3 1950 1.6 0.28 0.32 1.4 0.57
4 1951 1.9 0.30 0.39 1.5 0.56
5 1952 2.3 0.31 0.36 1.8 0.57
6 1953 2.7 0.32 0.36 1.9 0.71</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data I/O:讀取網頁內容(Lite)</h2></hgroup><article id="data-iolite">
</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://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")
# 如果你是windows, 這邊會遇到編碼問題,請加:
# web_page <- readLines("http://www.319papago.idv.tw/lifeinfo/chingshin/chingshin-02.html",encoding = 'UTF-8')
matches <- gregexpr("台北市南港區[\u4E00-\u9FA5|0-9]+", web_page)
tmp <- regmatches(web_page, matches)
unlist(tmp) # 把 list 轉成 vector</pre>
<pre >[1] "台北市南港區南港路3段20號" "台北市南港區南港路一段154號"</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p>其中:</p>
<pre >[\u4E00-\u9FA5] :表示所有中文字符
| :表示 或
[0-9] :含數字之字串
[a]+ :一或多個 a</pre>
<p>此寫法為 <strong>正規表示法</strong>,後續將會在 <strong>字串處理</strong> 的主題中跟各位介紹。</p>
</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)
head(unlist(tmp))</pre>
<pre >[1] "02-28761717" "02-28311515" "02-28805757" "02-28829191" "02-28126988"
[6] "02-28835757"</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data I/O : 檢視資料</h2></hgroup><article id="data-io-">
</article></slide><slide class=''><hgroup><h2>Recap 一下 Data I/O 我們學了什麼?</h2></hgroup><article id="recap--data-io-">
<p><strong>Data ETL 的第一步:輸入資料</strong></p>
<ul>
<li>設定資料路徑:<code>getwd()</code> & <code>setwd()</code></li>
<li>輸入不同資料型態:
<ul>
<li>表格式: <code>read.csv()</code> & <code>read.table()</code></li>
<li>網頁: <code>readLines()</code></li>
<li>其他軟體:<code>read.sas7bdat()</code> & <code>read.spss()</code></li>
<li>注意編碼、路徑設定、資料內容有沒有錯誤</li>
</ul></li>
<li>輸出資料:<code>write.csv()</code></li>
</ul>
</article></slide><slide class=''><hgroup><h2>輸入資料後下一步:檢視資料有無異常</h2></hgroup><article >
<p>輸入資料後,我們才準備正要開始 ETL 呢!</p>
<p>複習一下常用的資料檢視方式:</p>
<ul>
<li>總覽
<ul>
<li><code>head()</code>, <code>tail()</code>:抓前五筆、後五筆資料</li>
<li><code>str()</code>, <code>summary()</code>:檢視資料的結構、簡單敘述性統計</li>
<li><code>View()</code>:自由瀏覽</li>
</ul></li>
<li>單一欄位檢視
<ul>
<li><code>unique()</code>:檢視類別型欄位</li>
<li><code>table()</code>:檢視類別型欄位</li>
</ul></li>
</ul>
</article></slide><slide class=''><hgroup><h2>資料總覽</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>head(transaction)</pre>
<pre > X city district trac_year trac_month trac_type
1 1 臺北市 士林區 102 1 房地(土地+建物)
2 2 臺北市 中山區 102 1 房地(土地+建物)
3 3 臺北市 中山區 102 1 房地(土地+建物)+車位
4 4 臺北市 北投區 102 1 房地(土地+建物)
trac_content use_type build_type build_ymd area_land
1 土地1建物1車位0 住 公寓(5樓含以下無電梯) 701109 35.0
2 土地1建物1車位0 商 住宅大樓(11層含以上有電梯) 701228 10.7
3 土地1建物1車位1 商 套房(1房1廳1衛) 970114 8.5
4 土地1建物1車位0 商 住宅大樓(11層含以上有電梯) 851218 4.7
area_build area_park price_total price_unit age
1 61 0.0 6380000 105263 32
2 104 0.0 12010000 114928 32
3 52 8.6 10080000 194070 5
4 39 0.0 4600000 116900 17
[ reached getOption("max.print") -- omitted 2 rows ]</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'>tail(transaction)</pre>
<pre > X city district trac_year trac_month trac_type
153593 153593 新北市 三重區 102 4 房地(土地+建物)
153594 153594 新北市 三重區 102 6 房地(土地+建物)+車位
153595 153595 新北市 三重區 102 3 房地(土地+建物)
153596 153596 新北市 三重區 102 3 房地(土地+建物)
trac_content use_type build_type build_ymd
153593 土地1建物1車位0 住 住宅大樓(11層含以上有電梯) 1021220
153594 土地1建物1車位1 住 住宅大樓(11層含以上有電梯) 1021220
153595 土地1建物1車位0 住 住宅大樓(11層含以上有電梯) 1021220
153596 土地1建物1車位0 住 住宅大樓(11層含以上有電梯) 1021220
area_land area_build area_park price_total price_unit age
153593 10.6 81 0 11680000 143471 0
153594 12.5 97 0 15390000 158382 0
153595 8.3 64 0 9000000 140034 0
153596 8.3 64 0 9200000 143146 0
[ reached getOption("max.print") -- omitted 2 rows ]</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p><code>str()</code> 會檢視資料中每個欄位的:型態(int, Factor, chr, …)以及值</p>
<pre class = 'prettyprint lang-r'>str(transaction)</pre>
<center>
<img src = 'img/str_example.jpg' height = 400>
</center>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p><code>summary</code> 會檢視資料中每個欄位敘述性統計(值的分佈)</p>
<pre class = 'prettyprint lang-r'>summary(transaction)</pre>
<center>
<img src = 'img/summary_example.jpg' height = 300>
</center>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p><code>View()</code></p>
<pre class = 'prettyprint lang-r'>View(transaction)</pre>
<center>
<img src = 'img/view_data.jpg' height = 400>
</center>
</article></slide><slide class=''><hgroup><h2>單一欄位檢視</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>table(transaction$city)</pre>
<pre >
新北市 臺中市 臺北市 高雄市
57418 37482 24238 34460 </pre>
<pre class = 'prettyprint lang-r'>unique(transaction$district)</pre>
<pre > [1] 士林區 中山區 北投區 南港區 內湖區 中正區 信義區 文山區 松山區 萬華區
[11] 大安區 大同區 北區 大里區 西屯區 太平區 南屯區 沙鹿區 東勢區 西區
[21] 豐原區 中區 北屯區 龍井區 后里區 烏日區 大雅區 南區 潭子區 大甲區
[31] 神岡區 東區 清水區 霧峰區 大肚區 梧棲區 新社區 外埔區 石岡區 左營區
[41] 苓雅區 鼓山區 小港區 三民區 鳳山區 楠梓區 新興區 前鎮區 路竹區 仁武區
[51] 林園區 前金區 岡山區 大寮區 鳥松區 大社區 鹽埕區 橋頭區 湖內區 阿蓮區
[61] 茄萣區 大樹區 梓官區 美濃區 旗津區 燕巢區 旗山區 永安區 六龜區 甲仙區
[71] 彌陀區 桃源區 中和區 三峽區 土城區
[ reached getOption("max.print") -- omitted 24 entries ]
99 Levels: 三峽區 三民區 三芝區 三重區 中區 中和區 中山區 ... 龍井區</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data Manipulation</h2></hgroup><article id="data-manipulation">
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data Manipulation: Pipe Line Coding Style</h2></hgroup><article id="data-manipulation-pipe-line-coding-style">
</article></slide><slide class=''><hgroup><h2>為什麼這邊要教 Pipeline Coding style ?</h2></hgroup><article id="-pipeline-coding-style">
<p>根據<del>不具名</del>調查指出,寫程式花了將近 80% 時間在思考<strong>如何命名物件名稱</strong>。</p>
<p>命名物件名稱是門藝術,當然我們也可以隨性命名:</p>
<pre class = 'prettyprint lang-r'>## 假設我們現在要做這件事情
species <- iris$Species
species_again <- as.character(species)
species_again_again <- table(species_again)
species_again_again</pre>
<pre >species_again
setosa versicolor virginica
50 50 50 </pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p>當然我們也可以這樣改寫,節省命名的時間:</p>
<pre class = 'prettyprint lang-r'>table(as.character(iris$Species))</pre>
<pre >
setosa versicolor virginica
50 50 50 </pre>
</article></slide><slide class=''><hgroup><h2>為什麼這邊要教 Pipeline Coding style ?</h2></hgroup><article id="-pipeline-coding-style-1">
<p>當整份 <code>.r</code> 檔都是這樣的寫法時,你會發現幾個問題:</p>
<ul>
<li>解讀程式碼不直覺:我們習慣從外往內解讀 <code>table -> as.character -> iris$...</code></li>
<li>要改寫程式碼也較不容易</li>
</ul>
<p>所以我們需要一個更直覺的工具幫助我們解決這些問題 …</p>
</article></slide><slide class=''><hgroup><h2>2014 年最有影響的套件之一:magrittr</h2></hgroup><article id="magrittr">
<ul>
<li>壓縮的程式碼不好讀</li>
<li>展開的程式碼會產生很多暫存變數<br/><br/>
<h3>
<strong>magrittr 套件聽到大家的聲音了!</strong> <img src = 'img/magrittr_logo.png'>
</h3></li>
<li>運用 <code>magrittr</code> 所開發的 <code>%>%</code> 進行 Pipeline coding style</li>
<li>養成 Pipeline Style 的 coding 習慣,上述問題迎刃而解!
<ul>
<li>Pipeline 快捷鍵(MAC):<code>command + shift + M</code></li>
<li>Pipeline 快捷鍵(WIN):<code>ctrl + shift + M</code></li>
</ul></li>
</ul>
</article></slide><slide class=''><hgroup><h2>基本算子 (<code>%>%</code>)</h2></hgroup><article >
<iframe src="https://giphy.com/embed/aeSS6NKPqOKQ" width="450" height="330" frameBorder="0" class="giphy-embed" allowFullScreen>
</iframe>
</article></slide><slide class=''><hgroup><h2>基本算子 (<code>%>%</code>)</h2></hgroup><article id="-1">
<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>
<li>更多 Pipeline 請參考 Johnson Hsieh 的<a href='https://rawgit.com/DataScienceHC/sandbox-for-datasci/master/%5B2015-08-12%5D%20R%20code%20with%20pipes/index.html' title=''>講義</a></li>
</ul>
<pre class = 'prettyprint lang-r'># install.packages("magrittr")
library(magrittr)
x <- 1:10
x %>% mean # 由左而右順序操作</pre>
<pre >[1] 5.5</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<pre class = 'prettyprint lang-r'># 指令壓縮
plot(density(sample(mtcars$mpg, size=10000, replace=TRUE),
kernel="gaussian"), col="red", main="density of mpg")</pre>
<p><img src="DataETL_files/figure-html/unnamed-chunk-33-1.png" width="720" /></p>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<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="DataETL_files/figure-html/unnamed-chunk-34-1.png" width="720" /></p>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data Manipulation : 字串資料處理</h2></hgroup><article id="data-manipulation-">
</article></slide><slide class=''><hgroup><h2>tidyverse 套件</h2></hgroup><article id="tidyverse-">
<p><code>stringr</code> 是專門處理字串的一個知名套件,他與之後會介紹的強大套件 <code>dplyr</code>, <code>tidyr</code>, <code>ggplot2</code> 皆整合至 <code>tidyverse</code> 套件之中了。</p>
<center>
<img src = 'img/tidyverse.jpg'>
</center>
</article></slide><slide class=''><hgroup><h2>tidyverse 套件安裝</h2></hgroup><article id="tidyverse-">
<p>我們先安裝 <code>tidyverse</code> 套件:</p>
<pre class = 'prettyprint lang-r'>install.packages("tidyverse")</pre>
<pre class = 'prettyprint lang-r'>library(tidyverse)
library(stringr)</pre>
</article></slide><slide class=''><hgroup><h2>stringr 基本介紹</h2></hgroup><article id="stringr-">
<p><a href='http://stringr.tidyverse.org/reference/index.html' title=''>所有的 stringr 的 function</a> 皆用 <code>str_</code> 作為開頭(記得善用 Tab 鍵唷!)</p>
<p>我們用先前的 <code>transaction</code> 資料來練習一下字串處理吧!</p>
<p>以 <code>transaction</code> 的 <code>build_ymd</code> 建築年月日為例:</p>
<pre class = 'prettyprint lang-r'>build_ymd <- transaction$build_ymd
build_ymd %>% head()</pre>
<pre >[1] 701109 701228 970114 851218 970624 1010724</pre>
</article></slide><slide class=''><hgroup><h2>stringr:str_length & str_sub 字串長度與字串擷取</h2></hgroup><article id="stringrstr_length-str_sub-">
<ul>
<li><strong>目標:取出 build_ymd 的年、月、日</strong></li>
<li>注意:字串的長度不同(6與7)</li>
<li>善用 <code>?str_sub</code> 查詢 function 使用方法</li>
</ul>
<pre class = 'prettyprint lang-r'>year <- ifelse(str_length(build_ymd) == 6,
str_sub(build_ymd, 1,2),
build_ymd %>% str_sub(1,3)) # 這兩種寫法都可以
year_unique <- year %>% unique()
year_unique</pre>
<pre > [1] "70" "97" "85" "101" "100" "87" "98" "69" "86" "74" "78"
[12] "99" "67" "90" "71" "75" "73" "68" "94" "62" "10" "92"
[23] "82" "93" "88" "76" "60" "96" "66" "83" "61" "81" "72"
[34] "95" "65" "89" "79" "64" "63" "57" "80" "84" "58" "91"
[45] "77" "56" "59" "55" "51" "49" "50" "45" "53" "102" "15"
[56] "47" "54" "23" "52" "41" "12" "44" "17" "46" "42" "48"
[67] "38" "27" "24" "103" "35" "30" "22" "21" "40"
[ reached getOption("max.print") -- omitted 11 entries ]</pre>
</article></slide><slide class=''><hgroup><h2></h2></hgroup><article >
<p>若不寫 ifelse, 我們也可以:</p>
<ul>
<li>start = 1 : 從第一個開始</li>
<li>end = -5 : 到倒數第五個</li>
</ul>
<pre class = 'prettyprint lang-r'>year_unique2 <- str_sub(build_ymd, 1,-5) %>% unique()
any(year_unique != year_unique2) # check 有沒有任何一個不一樣</pre>
<pre >[1] FALSE</pre>
</article></slide><slide class=''><hgroup><h2>stringr : str_detect 字串檢驗</h2></hgroup><article id="stringr-str_detect-">
<ul>
<li><strong>目標:從 build_type 中判斷是否有電梯</strong></li>
<li>善用 <code>?str_detect</code> 查詢 function 使用方法</li>
</ul>
<pre class = 'prettyprint lang-r'>build_type_vector <- transaction$build_type
build_type_vector %>% str_detect('有電梯') %>% table</pre>
<pre >.
FALSE TRUE
62508 91090 </pre>
</article></slide><slide class=''><hgroup><h2>stringr : str_split 字串分割</h2></hgroup><article id="stringr-str_split-">
<ul>
<li><strong>目標:根據 fruit 中的 and 進行切割</strong></li>
<li>善用 <code>?str_split</code> 查詢 function 使用方法</li>
</ul>
<pre class = 'prettyprint lang-r'>fruits <- c(
"apples and oranges and pears and bananas",
"pineapples and mangos and guavas")
str_split(fruits, " and ")</pre>
<pre >[[1]]
[1] "apples" "oranges" "pears" "bananas"
[[2]]
[1] "pineapples" "mangos" "guavas" </pre>
<pre class = 'prettyprint lang-r'>str_split(fruits, " and ", simplify = TRUE) # simplify = T > 回傳矩陣</pre>
<pre > [,1] [,2] [,3] [,4]
[1,] "apples" "oranges" "pears" "bananas"
[2,] "pineapples" "mangos" "guavas" "" </pre>
</article></slide><slide class=''><hgroup><h2>stringr : str_replace 字串取代</h2></hgroup><article id="stringr-str_replace-">
<ul>
<li><strong>目標:將 trac_type 中的<code>房地</code>拿掉</strong></li>
<li>善用 <code>?str_replace</code> 查詢 function 使用方法</li>
</ul>
<pre class = 'prettyprint lang-r'>trac_type <- transaction$trac_type
trac_type %>% str_replace('房地','') %>% head</pre>
<pre >[1] "(土地+建物)" "(土地+建物)" "(土地+建物)+車位"
[4] "(土地+建物)" "(土地+建物)" "(土地+建物)+車位"</pre>
</article></slide><slide class=''><hgroup><h2>stringr : str_replace 字串取代</h2></hgroup><article id="stringr-str_replace--1" class="build">
<ul>
<li>目標:將 trac_type 中的<code>(土地+建物)</code>拿掉</li>
<li>善用 <code>?str_replace</code> 查詢 function 使用方法</li>
</ul>
<pre class = 'prettyprint lang-r'>trac_type <- transaction$trac_type
trac_type %>% str_replace('(土地+建物)','') %>% head</pre>
<pre >[1] "房地(土地+建物)" "房地(土地+建物)" "房地(土地+建物)+車位"
[4] "房地(土地+建物)" "房地(土地+建物)" "房地(土地+建物)+車位"</pre>
<center>
<img src='http://i.imgur.com/u4N3wpJ.jpg'>
</center>
</article></slide><slide class=''><hgroup><h2>正規表示法 Regular Expression</h2></hgroup><article id="-regular-expression">
<p>正規表示法是一種描述文字模式的語言,可以讓我們撰寫程式來自文字中比對、取代甚至是抽取各種資訊。</p>
<p><a href='http://datascienceandr.org/articles/RegularExpression.html' title=''>參考 Wush Wu 所撰寫的教材</a>,開頭或結尾的簡單的例子:</p>
<ul>
<li><code>^AA</code>:表示以<code>AA</code>為開頭的規則</li>
<li><code>AA$</code>:表示以<code>AA</code>為結尾的規則</li>
</ul>
<pre class = 'prettyprint lang-r'>str_detect(c('AA1','A2','V3','AAA4','ACA21'),'^AA')</pre>
<pre >[1] TRUE FALSE FALSE TRUE FALSE</pre>
<pre class = 'prettyprint lang-r'>str_detect(c('AA1','A2','V3','AAA4','ACA21'),'1$')</pre>
<pre >[1] TRUE FALSE FALSE FALSE TRUE</pre>
</article></slide><slide class=''><hgroup><h2><code>(土地+建物)</code>無法辨識的原因</h2></hgroup><article >
<p>stringr 中的 <code>pattern</code>參數預設使用正規表示法,剛剛我們所要比對的規則<code>(土地+建物)</code>中的<code>(</code>,<code>+</code>,<code>)</code>皆是正規表示法中的特殊符號,如果這些剛好是要比對的文字,那就要加上跳脫字元"\"。又剛好跳脫字元"\"也是R 的字串的跳脫字元,所以我們在輸入時,一個"\"就要輸入兩次。</p>
<pre class = 'prettyprint lang-r'>trac_type %>% str_replace('\\(土地\\+建物\\)','') %>% head</pre>
<pre >[1] "房地" "房地" "房地+車位" "房地" "房地" "房地+車位"</pre>
</article></slide><slide class='segue dark nobackground level1'><hgroup class = 'auto-fadein'><h2>Data Manipulation : 資料基本操作</h2></hgroup><article id="data-manipulation-">
</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("dplyr", 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>filter</code> 對列做篩選 (row)</li>
<li><code>select</code> 對欄做篩選 (column)</li>
<li><code>arrange</code> 排列</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>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">
<ul>
<li>目標:取出 city 為 '臺北市'</li>
</ul>
<pre class = 'prettyprint lang-r'>library(dplyr) #載入套件
transaction %>%
filter(city == '臺北市') %>% head</pre>
<center>
<img src = 'img/filter_view.jpg' height = 300>
</center>
</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">
<ul>
<li>目標:取出 <code>city</code>, <code>district</code>, <code>price_total</code> 欄位</li>
</ul>
<pre class = 'prettyprint lang-r'>transaction %>%
select(city, district, price_total) %>%
head()</pre>
<pre > city district price_total
1 臺北市 士林區 6380000
2 臺北市 中山區 12010000
3 臺北市 中山區 10080000
4 臺北市 北投區 4600000
5 臺北市 中山區 23800000
6 臺北市 北投區 62000000</pre>
</article></slide><slide class=''><hgroup><h2><code>select</code> : 對行做選取</h2></hgroup><article id="select--2">
<p>也可以用負號<code>-</code>執行反向選取</p>
<pre class = 'prettyprint lang-r'>transaction %>%
select(-c(city, district, price_total))%>%
head()</pre>
<center>
<img src = 'img/select_view.jpg' height = 300>
</center>
</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><code>arrange</code> : 資料排序</h2></hgroup><article id="arrange--1">
<ul>
<li>目標:按照總價格 <code>price_total</code> 由小到大進行排序</li>
<li>注意:可以發現到 price_total 似乎有一些值不合理?(0)如何檢驗? -> ETL</li>
</ul>
<pre class = 'prettyprint lang-r'>transaction %>% arrange(price_total) %>% select(X,city, price_total) %>% head</pre>
<pre > X city price_total
1 53063 臺中市 0
2 59110 臺中市 0
3 74325 高雄市 0
4 4381 臺北市 1