-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructured-Data.html
More file actions
3203 lines (2425 loc) · 117 KB
/
Copy pathStructured-Data.html
File metadata and controls
3203 lines (2425 loc) · 117 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>結構化的資料處理</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="generator" content="pandoc" />
<meta name="date" content="2019-05-11" />
<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: '結構化的資料處理',
useBuilds: true,
usePrettify: true,
enableSlideAreas: true,
enableTouch: true,
},
// Author information
presenters: [
{
name: 'Wush Wu' ,
company: '',
gplus: '',
twitter: '',
www: '',
github: ''
},
]
};
</script>
<link href="Structured-Data_files/ioslides-13.5.1/fonts/fonts.css" rel="stylesheet" />
<link href="Structured-Data_files/ioslides-13.5.1/theme/css/default.css" rel="stylesheet" />
<link href="Structured-Data_files/ioslides-13.5.1/theme/css/phone.css" rel="stylesheet" />
<script src="Structured-Data_files/ioslides-13.5.1/js/modernizr.custom.45394.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/prettify/prettify.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/prettify/lang-r.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/prettify/lang-yaml.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/hammer.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/slide-controller.js"></script>
<script src="Structured-Data_files/ioslides-13.5.1/js/slide-deck.js"></script>
<style type="text/css">
b, strong {
font-weight: bold;
}
em {
font-style: italic;
}
summary {
display: list-item;
}
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;
}
/* https://github.com/ropensci/plotly/pull/524#issuecomment-468142578 */
slide:not(.current) .plotly.html-widget{
display: block;
}
</style>
<link rel="stylesheet" href="structured-data.css" type="text/css" />
<link rel="stylesheet" href="../css/dsp.css" type="text/css" />
<link rel="stylesheet" href="../css/style.css" type="text/css" />
<link rel="stylesheet" href="../css/wush-custom.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;">2019-05-11</p>
</hgroup>
</slide>
<slide class=""><hgroup><h2>大綱</h2></hgroup><article id="section">
<ul>
<li>讀取CSV</li>
<li><code>dplyr</code> 的Verbs簡介</li>
<li>資料整合</li>
</ul>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>讀取CSV</h2></hgroup><article id="csv">
</article></slide><slide class=""><hgroup><h2>CSV的格式</h2></hgroup><article id="csv-1">
<ul>
<li>CSV: Comma-Separated Values</li>
</ul>
<p><img src='img/%E6%93%B7%E5%8F%96%E9%81%B8%E5%8F%96%E5%8D%80%E5%9F%9F_008.png' style='max-width: 100%; max-height: 100%; '></img></p>
</article></slide><slide class=""><hgroup><h2><code>read.csv</code></h2></hgroup><article id="read.csv">
<pre class = 'prettyprint lang-r'>read.csv("http://homepage.ntu.edu.tw/~wush978/rdataengineer/district_location.csv",
header = TRUE, nrows = 6)</pre>
<pre >## 行政區名 X_x0033_碼郵遞區號 中心點經度 中心點緯度
## 1 臺北市中正區 100 121.5199 25.03240
## 2 臺北市大同區 103 121.5130 25.06342
## 3 臺北市中山區 104 121.5382 25.06970
## 4 臺北市松山區 105 121.5576 25.05999
## 5 臺北市大安區 106 121.5434 25.02677
## 6 臺北市萬華區 108 121.4980 25.02859
## TGOS_URL
## 1 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 2 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 3 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 4 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 5 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 6 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false</pre>
</article></slide><slide class=""><hgroup><h2><code>read.table</code></h2></hgroup><article id="read.table">
<pre class = 'prettyprint lang-r'>read.csv("http://homepage.ntu.edu.tw/~wush978/rdataengineer/district_location.csv",
sep = ",", header = TRUE, nrows = 6)</pre>
<pre >## 行政區名 X_x0033_碼郵遞區號 中心點經度 中心點緯度
## 1 臺北市中正區 100 121.5199 25.03240
## 2 臺北市大同區 103 121.5130 25.06342
## 3 臺北市中山區 104 121.5382 25.06970
## 4 臺北市松山區 105 121.5576 25.05999
## 5 臺北市大安區 106 121.5434 25.02677
## 6 臺北市萬華區 108 121.4980 25.02859
## TGOS_URL
## 1 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 2 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 3 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 4 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 5 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false
## 6 http://tgos.nat.gov.tw/tgos/Web/MetaData/TGOS_MetaData_View.aspx?MID=9C715A5CD330360D355AE105F908B29E&SHOW_BACK_BUTTON=false</pre>
</article></slide><slide class=""><hgroup><h2><code>read.csv</code>與<code>read.table</code>的注意事項</h2></hgroup><article id="read.csvread.table">
<ul>
<li>預設會把<code>character vector</code>轉成<code>factor</code>型態
<ul>
<li>可以用參數<code>stringsAsFactors</code>控制</li>
</ul></li>
<li>實務上,R會猜欄位的型態(是<code>character</code>, <code>numeric</code>還是呢?),這很慢
<ul>
<li>可以用參數<code>colClasses</code>直接告訴R答案,讀大資料會快很多</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>讀取<code>CSV</code>的可能錯誤:編碼問題</h2></hgroup><article id="csv-2">
<pre class = 'prettyprint lang-r'>read.csv(url("http://homepage.ntu.edu.tw/~wush978/rdataengineer/district_location.csv", encoding = "BIG5"),
sep = ",", header = TRUE, nrows = 6)</pre>
<pre >## Warning in read.table(file = file, header = header, sep = sep, quote =
## quote, : 輸入連結 'http://homepage.ntu.edu.tw/~wush978/rdataengineer/
## district_location.csv' 中的輸入不正確</pre>
<pre >## Warning in read.table(file = file, header = header, sep = sep, quote
## = quote, : incomplete final line found by readTableHeader on 'http://
## homepage.ntu.edu.tw/~wush978/rdataengineer/district_location.csv'</pre>
<pre >## [1] 銵
## <0 rows> (or 0-length row.names)</pre>
<ul>
<li>解法:先用<code>readLines</code>處理或是用<code>readBin</code>處理後再讀取</li>
</ul>
</article></slide><slide class=""><hgroup><h2>讀取<code>CSV</code>的可能錯誤:欄位數量不一致</h2></hgroup><article id="csv-3">
<ul>
<li>內文包含<code>","</code>或分隔符號</li>
<li>資料有錯</li>
</ul>
<pre class = 'prettyprint lang-r'>read.csv("http://homepage.ntu.edu.tw/~wush978/rdataengineer/csv-error.csv")</pre>
<pre >## NAME ID
## Wush Chi-Hsuan Wu d12345678
## Hsieh Johnson d12345679</pre>
<ul>
<li>用<code>readLines</code>後手動用<code>strsplit</code>處理</li>
</ul>
</article></slide><slide class=""><hgroup><h2>小挑戰</h2></hgroup><article id="section-1">
<pre class = 'prettyprint lang-r'>x <- readLines("http://homepage.ntu.edu.tw/~wush978/rdataengineer/csv-error.csv")</pre>
<ul>
<li>請用程式找出兩位的學號</li>
</ul>
</article></slide><slide class=""><hgroup><h2>讀取CSV檔案的小撇步: colClasses 參數</h2></hgroup><article id="csv-colclasses-">
<ul>
<li><code>read.csv</code>與<code>read.table</code></li>
<li><code>colClasses</code>參數可以加速</li>
</ul>
<pre class = 'prettyprint lang-r'># source: https://support.spatialkey.com/spatialkey-sample-csv-data/
path <- tempfile(fileext = ".csv.gz")
download.file("http://homepage.ntu.edu.tw/~wush978/rdataengineer/FL_insurance_sample.csv.gz", destfile = path)</pre>
</article></slide><slide class=""><hgroup><h2>讀取CSV檔案的小撇步: colClasses 參數</h2></hgroup><article id="csv-colclasses--1">
<pre class = 'prettyprint lang-r'>readLines(gzfile(path), n = 6)</pre>
<pre >## [1] "policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity"
## [2] "119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1"
## [3] "448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3"
## [4] "206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1"
## [5] "333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3"
## [6] "172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1"</pre>
<pre class = 'prettyprint lang-r'>system.time(
FL <- read.csv(gzfile(path), header = TRUE)
)</pre>
<pre >## user system elapsed
## 0.288 0.010 0.298</pre>
</article></slide><slide class=""><hgroup><h2>讀取CSV檔案的小撇步: colClasses 參數</h2></hgroup><article id="csv-colclasses--2">
<pre class = 'prettyprint lang-r'>system.time({
FL.head <- read.csv(gzfile("FL_insurance_sample.csv.gz"), header = TRUE, nrows = 6)
.col <- sapply(FL.head, class)
.col[.col == "integer"] <- "numeric"
FL <- read.csv(gzfile("FL_insurance_sample.csv.gz"), header = TRUE, colClasses = .col)
})</pre>
<pre >## user system elapsed
## 0.170 0.008 0.184</pre>
</article></slide><slide class=""><hgroup><h2>讀取CSV檔案的小撇步: colClasses 參數</h2></hgroup><article id="csv-colclasses--3">
<pre class = 'prettyprint lang-r'>head(FL)</pre>
<pre >## policyID statecode county eq_site_limit hu_site_limit fl_site_limit
## 1 119736 FL CLAY COUNTY 498960.0 498960.00 498960.0
## 2 448094 FL CLAY COUNTY 1322376.3 1322376.30 1322376.3
## 3 206893 FL CLAY COUNTY 190724.4 190724.40 190724.4
## 4 333743 FL CLAY COUNTY 0.0 79520.76 0.0
## 5 172534 FL CLAY COUNTY 0.0 254281.50 0.0
## 6 785275 FL CLAY COUNTY 0.0 515035.62 0.0
## fr_site_limit tiv_2011 tiv_2012 eq_site_deductible
## 1 498960.0 498960.00 792148.90 0
## 2 1322376.3 1322376.30 1438163.57 0
## 3 190724.4 190724.40 192476.78 0
## 4 0.0 79520.76 86854.48 0
## 5 254281.5 254281.50 246144.49 0
## 6 0.0 515035.62 884419.17 0
## hu_site_deductible fl_site_deductible fr_site_deductible point_latitude
## 1 9979.2 0 0 30.10226
## 2 0.0 0 0 30.06394
## 3 0.0 0 0 30.08958
## 4 0.0 0 0 30.06324
## 5 0.0 0 0 30.06061
## 6 0.0 0 0 30.06324
## point_longitude line construction point_granularity
## 1 -81.71178 Residential Masonry 1
## 2 -81.70766 Residential Masonry 3
## 3 -81.70046 Residential Wood 1
## 4 -81.70770 Residential Wood 3
## 5 -81.70267 Residential Wood 1
## 6 -81.70770 Residential Masonry 3</pre>
</article></slide><slide class=""><hgroup><h2>Database</h2></hgroup><article id="database">
<ul>
<li>作業在 Windows 上的 3.5 系列有bug,修復中
<ul>
<li>請Windows使用者用別的版本的R跑作業</li>
</ul></li>
<li>什麼時候用 Database?
<ul>
<li>記憶體不夠</li>
<li>需要Transaction(多個操作中,只要一個失敗就全部復原)</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>Transaction</h2></hgroup><article id="transaction">
<ul>
<li>線上的資料處理上,在「容錯」上非常重要的「特性」</li>
<li>範例:
<ul>
<li>假設每小時產生一個檔案</li>
</ul></li>
</ul>
<pre class = 'prettyprint lang-r'>out.path <- sprintf("%s.csv", format(Sys.time(), "%Y-%m-%d-%H"))
# do something
write(data, file = out.path)</pre>
</article></slide><slide class=""><hgroup><h2>Transaction</h2></hgroup><article id="transaction-1">
<ul>
<li>線上的資料處理上,在「容錯」上非常重要的「特性」</li>
<li>範例:
<ul>
<li>假設每小時產生一個檔案</li>
<li>寫入到一半的時候發生錯誤(斷電、當機)</li>
<li>自動重開機後又繼續跑,產生新的檔案</li>
<li>哪些檔案是錯的?</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>Transaction</h2></hgroup><article id="transaction-2">
<pre class = 'prettyprint lang-r'>out.path <- format(Sys.time(), "%Y-%m-%d-%H.csv")
out.path.tmp <- paste(out.path, "tmp", sep = ".")
write(data, file = out.path.tmp)
# rename is transaction
file.rename(out.path.tmp, out.path)</pre>
</article></slide><slide class=""><hgroup><h2>XML Tables</h2></hgroup><article id="xml-tables">
<ul>
<li><code>XML::readHTMLTable</code></li>
<li><code>XML</code> 是比較老牌的XML處理工具
<ul>
<li>資料結構比<code>xml2</code>更難懂,但是比較穩(?)</li>
<li>如果資料來源是結構化的HTML表格,<code>XML::readHTMLTable</code>很方便</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>2018 縣市長大選台北即時估票</h2></hgroup><article id="section-2">
<ul>
<li><a href='https://gist.github.com/wush978/1619ddb3ed093a11febb0da592f5fc9d' title=''>外差估票程式</a>
<ol>
<li>從中選會的網頁上抓候選人的得票數</li>
<li>利用各正區現有的得票率當機準,依照「已開票」與「未開票」的比率放大</li>
<li>推估最終候選人的得票</li>
<li>可惜現在網頁格式不一致,所以會跑出NA XD</li>
</ol></li>
<li><a href='https://www.facebook.com/wush978/posts/2194489713895986' title=''>紀錄</a></li>
</ul>
</article></slide><slide class=""><hgroup><h2>data.frame</h2></hgroup><article id="data.frame">
<ul>
<li>由list物件擴充而成
<ul>
<li>list + attributes</li>
</ul></li>
<li>在R 語言中,處理「表格」(table)資料
<ul>
<li>表格 v.s. 矩陣、陣列</li>
</ul></li>
<li>視覺化:ggplot2</li>
<li>許多進階分析的入口
<ul>
<li>例:迴歸分析(<code>lm</code>)</li>
<li>將表格的變數轉換成數學上的矩陣:<code>model.matrix</code> \(\hat{\beta} = (X^TX)^{-1}(X^Ty)\)</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Create</h2></hgroup><article id="data.frame--create">
<ul>
<li>注意參數: <code>stringsAsFactors</code></li>
</ul>
<pre class = 'prettyprint lang-r'>data.frame(student.id = 1:5, math.score = rpois(5, 5))</pre>
<pre >## student.id math.score
## 1 1 3
## 2 2 8
## 3 3 3
## 4 4 4
## 5 5 5</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Create</h2></hgroup><article id="data.frame--create-1">
<pre class = 'prettyprint lang-r'>df <- read.csv(
url("https://raw.githubusercontent.com/wush978/DataScienceAndR/course/01-RBasic-07-Loading-Dataset/A_LVR_LAND_A.CSV", encoding = "BIG5"),
nrows = 6, header = TRUE)
df[,1:3]</pre>
<pre >## 鄉鎮市區 交易標的 土地區段位置或建物區門牌
## 1 文山區 房地(土地+建物) 臺北市文山區木柵路二段109巷100弄61~90號
## 2 中正區 房地(土地+建物) 臺北市中正區南海路1~30號
## 3 中正區 房地(土地+建物) 臺北市中正區重慶南路三段121~150號
## 4 文山區 房地(土地+建物) 臺北市文山區指南路三段32巷1~30號
## 5 文山區 房地(土地+建物) 臺北市文山區羅斯福路五段92巷1弄1~30號
## 6 文山區 房地(土地+建物) 臺北市文山區秀明路二段1~30號</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read">
<ul>
<li>list的Read: <code>[</code>、<code>[[</code>與<code>$</code></li>
</ul>
<pre class = 'prettyprint lang-r'>df["鄉鎮市區"]</pre>
<pre >## 鄉鎮市區
## 1 文山區
## 2 中正區
## 3 中正區
## 4 文山區
## 5 文山區
## 6 文山區</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-1">
<ul>
<li>list的Read: <code>[</code>、<code>[[</code>與<code>$</code></li>
</ul>
<pre class = 'prettyprint lang-r'>df[["鄉鎮市區"]]</pre>
<pre >## [1] 文山區 中正區 中正區 文山區 文山區 文山區
## Levels: 文山區 中正區</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-2">
<ul>
<li>list的Read: <code>[</code>、<code>[[</code>與<code>$</code></li>
</ul>
<pre class = 'prettyprint lang-r'>df$`鄉鎮市區`</pre>
<pre >## [1] 文山區 中正區 中正區 文山區 文山區 文山區
## Levels: 文山區 中正區</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-3">
<ul>
<li><code>[</code>: 仍然是data.frame</li>
<li><code>[[</code>、<code>$</code>: data.frame(list)會被打破</li>
</ul>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-4">
<ul>
<li>matrix的Read: <code>[</code></li>
</ul>
<pre class = 'prettyprint lang-r'>df[1,1]</pre>
<pre >## [1] 文山區
## Levels: 文山區 中正區</pre>
<pre class = 'prettyprint lang-r'>df[1:2,1]</pre>
<pre >## [1] 文山區 中正區
## Levels: 文山區 中正區</pre>
<pre class = 'prettyprint lang-r'>df[1,1:2]</pre>
<pre >## 鄉鎮市區 交易標的
## 1 文山區 房地(土地+建物)</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-5">
<ul>
<li>matrix的Read: <code>[</code>
<ul>
<li>應仍然是data.frame</li>
<li>參數<code>drop = TRUE</code>(預設)當欄位方向的維度為1時,會自動把data.frame轉成向量</li>
</ul></li>
</ul>
<pre class = 'prettyprint lang-r'>df[1:2,1:2]</pre>
<pre >## 鄉鎮市區 交易標的
## 1 文山區 房地(土地+建物)
## 2 中正區 房地(土地+建物)</pre>
<pre class = 'prettyprint lang-r'>df[1,1,drop = FALSE]</pre>
<pre >## 鄉鎮市區
## 1 文山區</pre>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Read</h2></hgroup><article id="data.frame--read-6">
<ul>
<li><code>drop = TRUE</code> 是好事嘛?
<ul>
<li>Hadley 主導的 <a href='https://www.tidyverse.org/' title=''>tidyverse</a></li>
<li>不是所有人都喜歡… <a href='https://stat.ethz.ch/pipermail/r-package-devel/2017q3/001896.html' title=''>tibbles are not data.frames</a></li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Update</h2></hgroup><article id="data.frame--update">
<ul>
<li><code>Read</code> + <code><-</code></li>
</ul>
<div class="columns-2">
<pre class = 'prettyprint lang-r'>df <- data.frame(
id = 1:5,
score = sample(1:10, 5, TRUE))
df</pre>
<pre >## id score
## 1 1 4
## 2 2 8
## 3 3 5
## 4 4 8
## 5 5 5</pre>
<pre class = 'prettyprint lang-r'>df$score <- scale(df$score)
df</pre>
<pre >## id score
## 1 1 -1.0690450
## 2 2 1.0690450
## 3 3 -0.5345225
## 4 4 1.0690450
## 5 5 -0.5345225</pre></div>
</article></slide><slide class=""><hgroup><h2>data.frame 的 Delete</h2></hgroup><article id="data.frame--delete">
<ul>
<li>反向Read</li>
<li>Read + <code><- NULL</code></li>
</ul>
<div class="columns-2">
<pre class = 'prettyprint lang-r'>df[-1,] # df[2:5,]</pre>
<pre >## id score
## 2 2 1.0690450
## 3 3 -0.5345225
## 4 4 1.0690450
## 5 5 -0.5345225</pre>
<pre class = 'prettyprint lang-r'>df$score <- NULL
df</pre>
<pre >## id
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5</pre></div>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-3">
<ul>
<li><code>iris</code>的資料中
<ul>
<li><code>Sepal.Length</code>的平均</li>
<li>各種<code>Species</code>的平均<code>Sepal.Length</code></li>
<li>建立新的欄位:<code>std.Sepal.Length</code>是標準化後的<code>Sepal.Length</code></li>
<li>建立新的欄位:<code>std.Sepal.Length</code>是依照個別<code>Species</code>作標準化後的<code>Sepal.Length</code></li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-4">
<ul>
<li>各種<code>Species</code>的平均<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>ans <- c()
for(.sp in levels(iris$Species)) {
.i <- iris$Species == .sp
ans[.sp] <- mean(iris$Sepal.Length[.i])
}
ans</pre>
<pre >## setosa versicolor virginica
## 5.006 5.936 6.588</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-5">
<ul>
<li>各種<code>Species</code>的平均<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'># you can use `lapply` and then `unlist`
sapply(levels(iris$Species), function(.sp) {
.i <- iris$Species == .sp
mean(iris$Sepal.Length[.i])
})</pre>
<pre >## setosa versicolor virginica
## 5.006 5.936 6.588</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-6">
<ul>
<li>各種<code>Species</code>的平均<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>. <- split(iris, iris$Species)
. <- lapply(., "[[", "Sepal.Length")
sapply(., mean)</pre>
<pre >## setosa versicolor virginica
## 5.006 5.936 6.588</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-7">
<ul>
<li>各種<code>Species</code>的平均<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>aggregate(Sepal.Length ~ Species, iris, mean)</pre>
<pre >## Species Sepal.Length
## 1 setosa 5.006
## 2 versicolor 5.936
## 3 virginica 6.588</pre>
<ul>
<li>要理解這段expression,同學需要學會:
<ul>
<li><code>formula object</code></li>
<li>Aggregation functions</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-8">
<ul>
<li>建立新的欄位:<code>std.Sepal.Length</code>是標準化後的<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>ans <- iris # to backup the original object, we modify after copying
. <- iris$Sepal.Length - mean(iris$Sepal.Length)
. <- . / sd(iris$Sepal.Length)
ans$std.Sepal.Length <- .
ans[c(1,2,51,52,101,102),6,drop=FALSE]</pre>
<pre >## std.Sepal.Length
## 1 -0.89767388
## 2 -1.13920048
## 51 1.39682886
## 52 0.67224905
## 101 0.55148575
## 102 -0.05233076</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-9">
<ul>
<li>建立新的欄位:<code>std.Sepal.Length</code>是標準化後的<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>ans <- iris # to backup the original object, we modify after copying
ans$std.Sepal.Length <- scale(iris$Sepal.Length)
ans[c(1,2,51,52,101,102),6,drop=FALSE]</pre>
<pre >## std.Sepal.Length
## 1 -0.89767388
## 2 -1.13920048
## 51 1.39682886
## 52 0.67224905
## 101 0.55148575
## 102 -0.05233076</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-10">
<ul>
<li>建立新的欄位:<code>std.Sepal.Length</code>是依照個別<code>Species</code>作標準化後的<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'>. <- iris$Sepal.Length
for(.sp in levels(iris$Species)) {
.i <- iris$Species == .sp
.[.i] <- scale(.[.i])
}
ans <- iris
ans$std.Sepal.Length <- .
ans[c(1,2,51,52,101,102),6,drop=FALSE]</pre>
<pre >## std.Sepal.Length
## 1 0.2666745
## 2 -0.3007180
## 51 2.0613318
## 52 0.8989266
## 101 -0.4529159
## 102 -1.2392283</pre>
</article></slide><slide class=""><hgroup><h2>範例</h2></hgroup><article id="section-11">
<ul>
<li>建立新的欄位:<code>std.Sepal.Length</code>是依照個別<code>Species</code>作標準化後的<code>Sepal.Length</code></li>
</ul>
<pre class = 'prettyprint lang-r'># This code will not work if the species is not ordered
. <- lapply(levels(iris$Species), function(.sp) {
.i <- iris$Species == .sp
scale(iris$Sepal.Length[.i])
})
ans <- iris
ans$std.Sepal.Length <- unlist(.)
ans[c(1,2,51,52,101,102),6,drop=FALSE]</pre>
<pre >## std.Sepal.Length
## 1 0.2666745
## 2 -0.3007180
## 51 2.0613318
## 52 0.8989266
## 101 -0.4529159
## 102 -1.2392283</pre>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2><a href='https://cran.r-project.org/package=dplyr' title=''>dplyr</a></h2></hgroup><article id="dplyr">
</article></slide><slide class=""><hgroup><h2>參考SQL 資料庫系統對結構化資料的操作做設計</h2></hgroup><article id="sql-">
<ul>
<li>一般企業儲存結構化資料的工具
<ul>
<li>儲存所有資料的工具</li>
<li>Transaction: 操作要嘛成功,要嘛無效</li>
</ul></li>
<li>SQL 資料庫的結構與操作是有數學代數在背後(<a href='https://en.wikipedia.org/wiki/Relational_algebra' title=''>Relational Algebra</a>)</li>
<li>R的data.frame v.s. SQL 資料庫
<ul>
<li>memory v.s. disk</li>
<li>indexing</li>
<li>column based v.s. row based</li>
</ul></li>
<li>一致的設計,讓同學可以透過<code>dplyr</code>的語法寫SQL
<ul>
<li>老師的經驗:比起用dplyr操作Database,還是直接寫 SQL 比較簡單… 但是可順便學SQL</li>
<li>有SQL經驗的同學可以快速上手R 的data.frame</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>dplyr 沒有完全相容於 data.frame</h2></hgroup><article id="dplyr--data.frame">
<ul>
<li>有時候,輸出的table不再是data.frame
<ul>
<li>為了效能</li>
<li>為了設計</li>
<li>因為Hadley(?)</li>
</ul></li>
<li>有必要時可以使用<code>as.data.frame</code></li>
</ul>
</article></slide><slide class=""><hgroup><h2>Single Table verbs</h2></hgroup><article id="single-table-verbs">
<ul>
<li>Read / Delete
<ul>
<li><code>filter</code>、<code>slice</code></li>
<li><code>select</code></li>
<li><code>sample_n</code>、<code>sample_frac</code></li>
</ul></li>
<li>Update
<ul>
<li><code>mutate</code></li>
<li><code>arrange</code></li>
</ul></li>
<li>Others
<ul>
<li><code>summarise</code></li>
<li><code>group_by</code></li>
<li><code>do</code></li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:filter</h2></hgroup><article id="filter">
<div class="columns-2" style="font-size-adjust:0.4;">
<pre class = 'prettyprint lang-r'>head(iris[iris$Sepal.Length > 3,1,drop=FALSE])</pre>
<pre >## Sepal.Length
## 1 5.1
## 2 4.9
## 3 4.7
## 4 4.6
## 5 5.0
## 6 5.4</pre>
<pre class = 'prettyprint lang-r'>head(filter(iris, Sepal.Length > 3)[,1,drop=FALSE])</pre>
<pre >## Sepal.Length
## 1 5.1
## 2 4.9
## 3 4.7
## 4 4.6
## 5 5.0
## 6 5.4</pre></div>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:filter</h2></hgroup><article id="filter-1">
<ul>
<li>在<code>dplyr</code>的函數中,<code>iris$</code>可以被省略
<ul>
<li>解析順序:欄位名稱 –> 變數名稱</li>
</ul></li>
<li><code>filter</code>的第一個參數是要處理的data.frame物件
<ul>
<li>所有的<code>dplyr</code>函數都是這樣設計</li>
</ul></li>
<li><code>filter</code>的其他參數必須是一個布林向量,並且長度一致
<ul>
<li>所有的這類參數,都是<code>TRUE</code>的位置,才會回傳</li>
</ul></li>
</ul>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:filter</h2></hgroup><article id="filter-2">
<pre class = 'prettyprint lang-r'>head(filter(iris, Sepal.Length > 3, Sepal.Width < 3.5))</pre>
<pre >## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.9 3.0 1.4 0.2 setosa
## 2 4.7 3.2 1.3 0.2 setosa
## 3 4.6 3.1 1.5 0.2 setosa
## 4 4.6 3.4 1.4 0.3 setosa
## 5 5.0 3.4 1.5 0.2 setosa
## 6 4.4 2.9 1.4 0.2 setosa</pre>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:filter</h2></hgroup><article id="filter-3">
<pre class = 'prettyprint lang-r'>head(filter(iris, Sepal.Length > 3, Sepal.Width < 3.5, Species == "versicolor"))</pre>
<pre >## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 7.0 3.2 4.7 1.4 versicolor
## 2 6.4 3.2 4.5 1.5 versicolor
## 3 6.9 3.1 4.9 1.5 versicolor
## 4 5.5 2.3 4.0 1.3 versicolor
## 5 6.5 2.8 4.6 1.5 versicolor
## 6 5.7 2.8 4.5 1.3 versicolor</pre>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:filter</h2></hgroup><article id="filter-4">
<pre class = 'prettyprint lang-r'>a <- iris$Sepal.Length + iris$Sepal.Width
head(filter(iris, Sepal.Length > 3, Sepal.Width < 3.5, a < 8))</pre>
<pre >## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.9 3.0 1.4 0.2 setosa
## 2 4.7 3.2 1.3 0.2 setosa
## 3 4.6 3.1 1.5 0.2 setosa
## 4 4.4 2.9 1.4 0.2 setosa
## 5 4.8 3.0 1.4 0.1 setosa
## 6 4.3 3.0 1.1 0.1 setosa</pre>
</article></slide><slide class=""><hgroup><h2>針對列作篩選:slice</h2></hgroup><article id="slice">
<pre class = 'prettyprint lang-r'>slice(iris, 1: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
## 6 5.4 3.9 1.7 0.4 setosa</pre>
</article></slide><slide class=""><hgroup><h2>針對列作抽樣:<code>sample_n</code>、<code>sample_frac</code></h2></hgroup><article id="sample_nsample_frac">
<div class="columns-2">
<pre class = 'prettyprint lang-r'>sample_n(iris, 6)[,1,drop=FALSE]</pre>
<pre >## Sepal.Length
## 1 5.6
## 2 6.5
## 3 4.4
## 4 5.7
## 5 6.1
## 6 6.0</pre>
<pre class = 'prettyprint lang-r'>sample_frac(iris, 0.04)[,1,drop=FALSE]</pre>
<pre >## Sepal.Length
## 1 7.6
## 2 6.3
## 3 6.0
## 4 6.3
## 5 6.0
## 6 6.6</pre></div>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select">
<pre class = 'prettyprint lang-r'>head(select(iris, Sepal.Length))</pre>
<pre >## Sepal.Length
## 1 5.1
## 2 4.9
## 3 4.7
## 4 4.6
## 5 5.0
## 6 5.4</pre>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select-1">
<pre class = 'prettyprint lang-r'>head(select(iris, Sepal.Length, Sepal.Width))</pre>
<pre >## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
## 4 4.6 3.1
## 5 5.0 3.6
## 6 5.4 3.9</pre>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select-2">
<pre class = 'prettyprint lang-r'>head(select(iris, starts_with("Sepal")))</pre>
<pre >## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
## 4 4.6 3.1
## 5 5.0 3.6
## 6 5.4 3.9</pre>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select-3">
<pre class = 'prettyprint lang-r'>head(select(iris, Sepal.Length:Petal.Length))</pre>
<pre >## Sepal.Length Sepal.Width Petal.Length
## 1 5.1 3.5 1.4
## 2 4.9 3.0 1.4
## 3 4.7 3.2 1.3
## 4 4.6 3.1 1.5
## 5 5.0 3.6 1.4
## 6 5.4 3.9 1.7</pre>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select-4">
<ul>
<li>反向操作</li>
</ul>
<pre class = 'prettyprint lang-r'>head(select(iris, -Sepal.Length))</pre>
<pre >## Sepal.Width Petal.Length Petal.Width Species
## 1 3.5 1.4 0.2 setosa
## 2 3.0 1.4 0.2 setosa
## 3 3.2 1.3 0.2 setosa
## 4 3.1 1.5 0.2 setosa
## 5 3.6 1.4 0.2 setosa
## 6 3.9 1.7 0.4 setosa</pre>
</article></slide><slide class=""><hgroup><h2>針對欄位作篩選:select</h2></hgroup><article id="select-5">
<ul>
<li>rename</li>
</ul>