forked from wush978/DataScienceAndR
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RBasic-DataStructure.html
1519 lines (1378 loc) · 47.3 KB
/
RBasic-DataStructure.html
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 的資料結構</title>
<meta charset="utf-8">
<meta name="description" content="R 的資料結構">
<meta name="author" content="Wush Wu">
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="libraries/frameworks/io2012-wush/css/default.css" media="all" >
<link rel="stylesheet" href="libraries/frameworks/io2012-wush/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="libraries/frameworks/io2012-wush/css/slidify.css" >
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/zenburn.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. --> <link rel=stylesheet href="./assets/css/custom.css"></link>
<link rel=stylesheet href="./assets/css/ribbons.css"></link>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="libraries/frameworks/io2012-wush/js/jquery-1.7.min.js"></script>
<!-- <script>window.jQuery || document.write('<script src="libraries/widgets/quiz/js/jquery.js"><\/script>')</script> -->
<script data-main="libraries/frameworks/io2012-wush/js/slides"
src="libraries/frameworks/io2012-wush/js/require-1.0.8.min.js">
</script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<hgroup class="auto-fadein">
<h1>R 的資料結構</h1>
<h2></h2>
<p>Wush Wu<br/>國立台灣大學</p>
</hgroup>
<article></article>
</slide>
<!-- SLIDES -->
<slide class="segue dark" id="slide-1" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>分析的資料型態:數值系統</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-2" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>數值系統的分類</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>名目資料(nomial)</li>
<li>順序資料(ordinal)</li>
<li>區間資料(interval)</li>
<li>比例資料(ratio)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-3" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>名目資料</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>性別</li>
<li>Domain</li>
<li>屬性的有無</li>
</ul>
<p><img src="./assets/fig/rbasic-datastructure-unnamed-chunk-1-1.png" alt="plot of chunk unnamed-chunk-1"></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-4" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>順序資料</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>硬度表</li>
<li>名次</li>
<li>排序表</li>
</ul>
<pre><code>[1] Qn1 Qn1 Qn1 Qn1 Qn1 Qn1
12 Levels: Qn1 < Qn2 < Qn3 < Qc1 < Qc3 < Qc2 < Mn3 < Mn2 < Mn1 < ... < Mc1
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-5" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>區間資料</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>溫度</li>
<li>時間</li>
</ul>
<p><img src="./assets/fig/rbasic-datastructure-unnamed-chunk-3-1.png" alt="plot of chunk unnamed-chunk-3"></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-6" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>比值資料</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>營收</li>
<li>股價</li>
</ul>
<p><img src="./assets/fig/rbasic-datastructure-unnamed-chunk-4-1.png" alt="plot of chunk unnamed-chunk-4"></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-7" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>R 的資料型態</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-8" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的資料結構概論</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>R 是一種程式語言,程式語言都有對應的資料結構</li>
<li><a href="https://github.com/wch/r-source/blob/trunk/src/include/Rinternals.h#L102">Rinternals.h</a>:</li>
</ul>
<pre><code class="c">typedef unsigned int SEXPTYPE;
#define NILSXP 0 /* nil = NULL */
#define SYMSXP 1 /* symbols */
#define LISTSXP 2 /* lists of dotted pairs */
#define CLOSXP 3 /* closures */
#define ENVSXP 4 /* environments */
...
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-9" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的資料結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>所有的東西都是一種「R 物件」
<ul>
<li><strong>資料</strong></li>
<li>函數</li>
<li>環境</li>
<li>外部指標</li>
</ul></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-10" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的物件結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>複雜的R 物件們都是由基礎的R 物件所組合的</li>
</ul>
<pre><code class="r">g <- lm(dist ~ speed, cars)
str(head(g))
</code></pre>
<pre><code>List of 6
$ coefficients : Named num [1:2] -17.58 3.93
..- attr(*, "names")= chr [1:2] "(Intercept)" "speed"
$ residuals : Named num [1:50] 3.85 11.85 -5.95 12.05 2.12 ...
..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
$ effects : Named num [1:50] -303.914 145.552 -8.115 9.885 0.194 ...
..- attr(*, "names")= chr [1:50] "(Intercept)" "speed" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:50] -1.85 -1.85 9.95 9.95 13.88 ...
..- attr(*, "names")= chr [1:50] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-11" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的物件結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<p><center><img src='assets/img/R-attributes.png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-12" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 中常見的原子物件</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>邏輯向量</li>
<li>整數向量</li>
<li>數值向量</li>
<li>字串向量</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-13" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的所有設計都是為了分析資料而生</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>資料的最小單位是「向量」</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-14" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>邏輯向量</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>用於作布林運算、流程控制</li>
</ul>
<pre><code class="r">c(T, F, TRUE, FALSE)
</code></pre>
<pre><code>[1] TRUE FALSE TRUE FALSE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-15" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>整數向量</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>每個整數佔用4 bytes</li>
</ul>
<pre><code class="r">c(1L, 2L, 3L, 4L, 0xaL)
</code></pre>
<pre><code>[1] 1 2 3 4 10
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-16" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>數值向量</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>每個數值佔用8 bytes(雙精確浮點數)</li>
</ul>
<pre><code class="r">c(1.0, .1, 1e-2, 1e2, 1.2e2)
</code></pre>
<pre><code>[1] 1.00 0.10 0.01 100.00 120.00
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-17" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>字串向量</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>NULL結尾的字串向量</li>
</ul>
<pre><code class="r">c("1", "a", "中文")
</code></pre>
<pre><code>[1] "1" "a" "中文"
</code></pre>
<pre><code class="r">c("a\0b")
</code></pre>
<p><font color="red">Error: nul character not allowed (line 1)</font></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-18" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>數值系統與原子資料結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>名目資料(nomial): 字串向量、邏輯向量</li>
<li>順序資料(ordinal): 無</li>
<li>區間資料(interval): 整數向量、數值向量</li>
<li>比例資料(ratio): 整數向量、數值向量</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-19" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>習題時間</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li><p>請同學完成以下的swirl課程,練習操作上述介紹的R 物件</p></li>
<li><p>RBasic-02-Data-Structure-Vectors</p></li>
<li><p>RBasic-03-Data-Structure-Object</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-20" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>R 的factor</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-21" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的物件結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<p><center><img src='assets/img/R-attributes.png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-22" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>內建的<code>factor</code>範例</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">head(CO2$Type)
</code></pre>
<pre><code>[1] Quebec Quebec Quebec Quebec Quebec Quebec
Levels: Quebec Mississippi
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-23" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>factor</code>的真相</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">dput(CO2$Type)
</code></pre>
<pre><code>structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Quebec", "Mississippi"
), class = "factor")
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-24" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>factor</code>的真相</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">attributes(CO2$Type)
</code></pre>
<pre><code>$levels
[1] "Quebec" "Mississippi"
$class
[1] "factor"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-25" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的歷史包袱</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li><code>dput</code>函數會輸出如<code>.Label</code>這種標籤,但是並不是真正的屬性標籤</li>
<li><p>以下內容擷取自<code>structure</code>的說明文件:</p>
<p>Adding a class "factor" will ensure that numeric codes are given integer storage mode.</p>
<p>For historical reasons (these names are used when deparsing), attributes ".Dim", ".Dimnames", ".Names", ".Tsp" and ".Label" are renamed to "dim", "dimnames", "names", "tsp" and "levels".</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-26" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>數值系統與資料結構</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>名目資料(nomial): 字串向量、邏輯向量、<em>factor</em></li>
<li>順序資料(ordinal): <em>factor</em></li>
<li>區間資料(interval): 整數向量、數值向量</li>
<li>比例資料(ratio): 整數向量、數值向量</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-27" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>習題時間</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>請同學完成<code>RBasic-04-Factors</code>,練習操作R 的factor物件</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-28" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>R 的Matrix與array</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-29" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Matrix</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> x <- matrix(1:4, 2, 2)
> x
[,1] [,2]
[1,] 1 3
[2,] 2 4
> class(x)
[1] "matrix"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-30" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Matrix與Attributes</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> attributes(x)
$dim
[1] 2 2
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-31" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Matrix與Attributes</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> attributes(x) <- NULL
> x # 同 1:4
[1] 1 2 3 4
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-32" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Matrix與Attributes</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<p><center><img src='assets/img/Matrix.png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-33" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Array與Attributes</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> attr(x, "dim") <- c(2, 2, 1)
> x # 同 1:4
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
> class(x)
[1] "array"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-34" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>為什麼要使用R 的Matrix?</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>各種方便操作的API</li>
<li>優化過的運算效能(BLAS)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-35" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>習題時間</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>請同學完成<code>RBasic-05-Arrays-Matrices</code>,練習操作R 的matrix和array物件</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-36" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>List: R 物件的向量</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-37" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>原子物件向量,具備有同質性</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>牽一髮而動全身</li>
</ul>
<pre><code class="r">> x <- 1:10
> class(x)
[1] "integer"
> x[1] <- "1"
> class(x)
[1] "character"
> x
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-38" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>實務的資料不只是單一型態</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>R 物件可以是任何型態的向量</li>
<li>R 物件的向量就是處理相異型態的工具</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-39" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>整數向量</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">1:5
</code></pre>
<p><center><img src='assets/img/IntegerVector.png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-40" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>List</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">x <- list(1:5, c("a", "b"))
</code></pre>
<p><center><img src='assets/img/List.png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-41" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>[</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">x[1]
</code></pre>
<p><center><img src='assets/img/List[1].png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-42" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>[[</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">x[[1]]
</code></pre>
<p><center><img src='assets/img/List[[1]].png' style='width: 100%'></img></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-43" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>List and <code>names</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> x <- list(1:5, c("a", "b"))
> x
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] "a" "b"
> attributes(x)
NULL
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-44" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>List and <code>names</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> x <- list(a = 1:5, b = c("a", "b"))
> x
$a
[1] 1 2 3 4 5
$b
[1] "a" "b"
> attributes(x)
$names
[1] "a" "b"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-45" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>$</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>取出對應名稱的元素</li>
</ul>
<pre><code class="r">> x <- list(a = 1:5, b = c("a", "b"))
> x$a
[1] 1 2 3 4 5
> x$b
[1] "a" "b"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-46" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>從List到<code>data.frame</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>List提供了處理異質資料的工具</li>
<li>List非常的泛用,甚至延生出R 的S3物件導向系統</li>
<li>但是對於結構化的資料,List不夠方便阿...
<ul>
<li>看看矩陣</li>
</ul></li>
<li><code>data.frame</code>是R 為了解決結構化資料所提出的解決方案</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-47" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>R 的 Data Frame</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-48" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Data Frame</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li><p>已經成為處理「結構化資料」的典範</p>
<p>The main driver for Distributed DataFrame is to have a cluster-based, big data representation that’s friendly to the RDBMSs and data science community. <strong>Specifically we leverage SQL’s table and R’s data.frame concepts, taking advantage of 30 years of SQL development and R’s accumulated data science wisdom</strong>. </p></li>
</ul>
<p><center>Source: <a href="http://ddf.io/design.html">http://ddf.io/design.html</a></center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-49" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>R 的Data Frame</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> head(iris)
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
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-50" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>Data Frame是一種List</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> class(iris)
[1] "data.frame"
> is.list(iris)
[1] TRUE
> head(iris[[1]])
[1] 5.1 4.9 4.7 4.6 5.0 5.4
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-51" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>Data Frame 提供了類似矩陣的API</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">> iris[1,]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
> iris[1,1]
[1] 5.1
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-52" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>習題時間</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>請同學完成<code>RBasic-06-List-DataFrame</code>的課程,實際操作List和Data.Frame</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-53" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>實務經驗</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-54" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>記憶體問題</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>R 是一個以記憶體為主的分析工具
<ul>
<li>R 假設你的記憶體是足夠的</li>
</ul></li>
<li>R 跑得很慢
<ul>
<li>CPU 不夠快(計算量太大)</li>
<li>記憶體不夠大(資料太大)</li>
</ul></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-55" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>如何估計記憶體的使用量?</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>向量就是陣列,加上一些其它的metadata(型態、attributes、...)</li>
<li>邏輯向量、整數向量和數值向量的空間大約為:\(長度 \times 單位空間\)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-56" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>object.size</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">object.size(logical(0))
</code></pre>
<pre><code>40 bytes
</code></pre>
<pre><code class="r">object.size(rep(TRUE, 1000))
</code></pre>
<pre><code>4040 bytes
</code></pre>
<pre><code class="r">object.size(rep(TRUE, 1e6))
</code></pre>
<pre><code>4000040 bytes
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-57" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>object.size</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">object.size(integer(0))
</code></pre>
<pre><code>40 bytes
</code></pre>
<pre><code class="r">object.size(seq(1L, by = 1L, length = 1e3))
</code></pre>
<pre><code>4040 bytes
</code></pre>
<pre><code class="r">object.size(seq(1L, by = 1L, length = 1e6))
</code></pre>
<pre><code>4000040 bytes
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-58" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2><code>object.size</code></h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">object.size(numeric(0))
</code></pre>
<pre><code>40 bytes
</code></pre>
<pre><code class="r">object.size(seq(0, by = 1, length = 1000))
</code></pre>
<pre><code>8040 bytes
</code></pre>
<pre><code class="r">object.size(seq(0, by = 1, length = 1e6))
</code></pre>
<pre><code>8000040 bytes
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-59" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>字串向量的記憶體用量不容易估計</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<pre><code class="r">speaker <- readLines("speaker.txt")
speaker[2]
</code></pre>
<pre><code>[1] "年會總召, 中央研究院資訊科學研究所/ 研究員"
</code></pre>
<pre><code class="r">length(speaker)
</code></pre>
<pre><code>[1] 216
</code></pre>
<pre><code class="r">file.size("speaker.txt")
</code></pre>
<pre><code>[1] 18464
</code></pre>
<pre><code class="r">object.size(speaker)
</code></pre>
<pre><code>24664 bytes
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-60" style="background-image: url('libraries/frameworks/io2012-wush/images/background.png'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;" >
<hgroup>
<h2>關於記憶體</h2>
</hgroup>
<article class = 'flexbox vcenter'>
<ul>
<li>事前估計需要的記憶體用量