-
Notifications
You must be signed in to change notification settings - Fork 0
/
slide.html
1402 lines (1291 loc) · 613 KB
/
slide.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>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="y__mattu" />
<title>初心者セッション</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="slide_files/reveal.js-3.3.0.1/css/reveal.css"/>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #ffffff;
color: #a0a0a0;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #a0a0a0; padding-left: 4px; }
div.sourceCode
{ color: #1f1c1b; background-color: #ffffff; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span { color: #1f1c1b; } /* Normal */
code span.al { color: #bf0303; background-color: #f7e6e6; font-weight: bold; } /* Alert */
code span.an { color: #ca60ca; } /* Annotation */
code span.at { color: #0057ae; } /* Attribute */
code span.bn { color: #b08000; } /* BaseN */
code span.bu { color: #644a9b; font-weight: bold; } /* BuiltIn */
code span.cf { color: #1f1c1b; font-weight: bold; } /* ControlFlow */
code span.ch { color: #924c9d; } /* Char */
code span.cn { color: #aa5500; } /* Constant */
code span.co { color: #898887; } /* Comment */
code span.cv { color: #0095ff; } /* CommentVar */
code span.do { color: #607880; } /* Documentation */
code span.dt { color: #0057ae; } /* DataType */
code span.dv { color: #b08000; } /* DecVal */
code span.er { color: #bf0303; text-decoration: underline; } /* Error */
code span.ex { color: #0095ff; font-weight: bold; } /* Extension */
code span.fl { color: #b08000; } /* Float */
code span.fu { color: #644a9b; } /* Function */
code span.im { color: #ff5500; } /* Import */
code span.in { color: #b08000; } /* Information */
code span.kw { color: #1f1c1b; font-weight: bold; } /* Keyword */
code span.op { color: #1f1c1b; } /* Operator */
code span.ot { color: #006e28; } /* Other */
code span.pp { color: #006e28; } /* Preprocessor */
code span.re { color: #0057ae; background-color: #e0e9f8; } /* RegionMarker */
code span.sc { color: #3daee9; } /* SpecialChar */
code span.ss { color: #ff5500; } /* SpecialString */
code span.st { color: #bf0303; } /* String */
code span.va { color: #0057ae; } /* Variable */
code span.vs { color: #bf0303; } /* VerbatimString */
code span.wa { color: #bf0303; } /* Warning */
</style>
<link rel="stylesheet" href="slide_files/reveal.js-3.3.0.1/css/theme/sky.css" id="theme">
<!-- some tweaks to reveal css -->
<style type="text/css">
.reveal h1 { font-size: 2.0em; }
.reveal h2 { font-size: 1.5em; }
.reveal h3 { font-size: 1.25em; }
.reveal h4 { font-size: 1em; }
.reveal .slides>section,
.reveal .slides>section>section {
padding: 0px 0px;
}
.reveal table {
border-width: 1px;
border-spacing: 2px;
border-style: dotted;
border-color: gray;
border-collapse: collapse;
font-size: 0.7em;
}
.reveal table th {
border-width: 1px;
padding-left: 10px;
padding-right: 25px;
font-weight: bold;
border-style: dotted;
border-color: gray;
}
.reveal table td {
border-width: 1px;
padding-left: 10px;
padding-right: 25px;
border-style: dotted;
border-color: gray;
}
</style>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet" href="for_revealjs.css"/>
<!-- Printing and PDF exports -->
<script id="paper-css" type="application/dynamic-css">
/* Default Print Stylesheet Template
by Rob Glazebrook of CSSnewbie.com
Last Updated: June 4, 2008
Feel free (nay, compelled) to edit, append, and
manipulate this file as you see fit. */
@media print {
/* SECTION 1: Set default width, margin, float, and
background. This prevents elements from extending
beyond the edge of the printed page, and prevents
unnecessary background images from printing */
html {
background: #fff;
width: auto;
height: auto;
overflow: visible;
}
body {
background: #fff;
font-size: 20pt;
width: auto;
height: auto;
border: 0;
margin: 0 5%;
padding: 0;
overflow: visible;
float: none !important;
}
/* SECTION 2: Remove any elements not needed in print.
This would include navigation, ads, sidebars, etc. */
.nestedarrow,
.controls,
.fork-reveal,
.share-reveal,
.state-background,
.reveal .progress,
.reveal .backgrounds {
display: none !important;
}
/* SECTION 3: Set body font face, size, and color.
Consider using a serif font for readability. */
body, p, td, li, div {
font-size: 20pt!important;
font-family: Georgia, "Times New Roman", Times, serif !important;
color: #000;
}
/* SECTION 4: Set heading font face, sizes, and color.
Differentiate your headings from your body text.
Perhaps use a large sans-serif for distinction. */
h1,h2,h3,h4,h5,h6 {
color: #000!important;
height: auto;
line-height: normal;
font-family: Georgia, "Times New Roman", Times, serif !important;
text-shadow: 0 0 0 #000 !important;
text-align: left;
letter-spacing: normal;
}
/* Need to reduce the size of the fonts for printing */
h1 { font-size: 28pt !important; }
h2 { font-size: 24pt !important; }
h3 { font-size: 22pt !important; }
h4 { font-size: 22pt !important; font-variant: small-caps; }
h5 { font-size: 21pt !important; }
h6 { font-size: 20pt !important; font-style: italic; }
/* SECTION 5: Make hyperlinks more usable.
Ensure links are underlined, and consider appending
the URL to the end of the link for usability. */
a:link,
a:visited {
color: #000 !important;
font-weight: bold;
text-decoration: underline;
}
/*
.reveal a:link:after,
.reveal a:visited:after {
content: " (" attr(href) ") ";
color: #222 !important;
font-size: 90%;
}
*/
/* SECTION 6: more reveal.js specific additions by @skypanther */
ul, ol, div, p {
visibility: visible;
position: static;
width: auto;
height: auto;
display: block;
overflow: visible;
margin: 0;
text-align: left !important;
}
.reveal pre,
.reveal table {
margin-left: 0;
margin-right: 0;
}
.reveal pre code {
padding: 20px;
border: 1px solid #ddd;
}
.reveal blockquote {
margin: 20px 0;
}
.reveal .slides {
position: static !important;
width: auto !important;
height: auto !important;
left: 0 !important;
top: 0 !important;
margin-left: 0 !important;
margin-top: 0 !important;
padding: 0 !important;
zoom: 1 !important;
overflow: visible !important;
display: block !important;
text-align: left !important;
-webkit-perspective: none;
-moz-perspective: none;
-ms-perspective: none;
perspective: none;
-webkit-perspective-origin: 50% 50%;
-moz-perspective-origin: 50% 50%;
-ms-perspective-origin: 50% 50%;
perspective-origin: 50% 50%;
}
.reveal .slides section {
visibility: visible !important;
position: static !important;
width: auto !important;
height: auto !important;
display: block !important;
overflow: visible !important;
left: 0 !important;
top: 0 !important;
margin-left: 0 !important;
margin-top: 0 !important;
padding: 60px 20px !important;
z-index: auto !important;
opacity: 1 !important;
page-break-after: always !important;
-webkit-transform-style: flat !important;
-moz-transform-style: flat !important;
-ms-transform-style: flat !important;
transform-style: flat !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
transform: none !important;
-webkit-transition: none !important;
-moz-transition: none !important;
-ms-transition: none !important;
transition: none !important;
}
.reveal .slides section.stack {
padding: 0 !important;
}
.reveal section:last-of-type {
page-break-after: avoid !important;
}
.reveal section .fragment {
opacity: 1 !important;
visibility: visible !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
transform: none !important;
}
.reveal section img {
display: block;
margin: 15px 0px;
background: rgba(255,255,255,1);
border: 1px solid #666;
box-shadow: none;
}
.reveal section small {
font-size: 0.8em;
}
}
</script>
<script id="pdf-css" type="application/dynamic-css">
/**
* This stylesheet is used to print reveal.js
* presentations to PDF.
*
* https://github.com/hakimel/reveal.js#pdf-export
*/
* {
-webkit-print-color-adjust: exact;
}
body {
margin: 0 auto !important;
border: 0;
padding: 0;
float: none !important;
overflow: visible;
}
html {
width: 100%;
height: 100%;
overflow: visible;
}
/* Remove any elements not needed in print. */
.nestedarrow,
.reveal .controls,
.reveal .progress,
.reveal .playback,
.reveal.overview,
.fork-reveal,
.share-reveal,
.state-background {
display: none !important;
}
h1, h2, h3, h4, h5, h6 {
text-shadow: 0 0 0 #000 !important;
}
.reveal pre code {
overflow: hidden !important;
font-family: Courier, 'Courier New', monospace !important;
}
ul, ol, div, p {
visibility: visible;
position: static;
width: auto;
height: auto;
display: block;
overflow: visible;
margin: auto;
}
.reveal {
width: auto !important;
height: auto !important;
overflow: hidden !important;
}
.reveal .slides {
position: static;
width: 100%;
height: auto;
left: auto;
top: auto;
margin: 0 !important;
padding: 0 !important;
overflow: visible;
display: block;
-webkit-perspective: none;
-moz-perspective: none;
-ms-perspective: none;
perspective: none;
-webkit-perspective-origin: 50% 50%; /* there isn't a none/auto value but 50-50 is the default */
-moz-perspective-origin: 50% 50%;
-ms-perspective-origin: 50% 50%;
perspective-origin: 50% 50%;
}
.reveal .slides section {
page-break-after: always !important;
visibility: visible !important;
position: relative !important;
display: block !important;
position: relative !important;
margin: 0 !important;
padding: 0 !important;
box-sizing: border-box !important;
min-height: 1px;
opacity: 1 !important;
-webkit-transform-style: flat !important;
-moz-transform-style: flat !important;
-ms-transform-style: flat !important;
transform-style: flat !important;
-webkit-transform: none !important;
-moz-transform: none !important;
-ms-transform: none !important;
transform: none !important;
}
.reveal section.stack {
margin: 0 !important;
padding: 0 !important;
page-break-after: avoid !important;
height: auto !important;
min-height: auto !important;
}
.reveal img {
box-shadow: none;
}
.reveal .roll {
overflow: visible;
line-height: 1em;
}
/* Slide backgrounds are placed inside of their slide when exporting to PDF */
.reveal section .slide-background {
display: block !important;
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: -1;
}
/* All elements should be above the slide-background */
.reveal section>* {
position: relative;
z-index: 1;
}
/* Display slide speaker notes when 'showNotes' is enabled */
.reveal .speaker-notes-pdf {
display: block;
width: 100%;
max-height: none;
left: auto;
top: auto;
z-index: 100;
}
/* Display slide numbers when 'slideNumber' is enabled */
.reveal .slide-number-pdf {
display: block;
position: absolute;
font-size: 14px;
}
</script>
<script>
var style = document.createElement( 'style' );
style.type = 'text/css';
var style_script_id = window.location.search.match( /print-pdf/gi ) ? 'pdf-css' : 'paper-css';
var style_script = document.getElementById(style_script_id).text;
style.innerHTML = style_script;
document.getElementsByTagName('head')[0].appendChild(style);
</script>
<script src="slide_files/header-attrs-2.6/header-attrs.js"></script>
<link href="slide_files/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="slide_files/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<script src="slide_files/htmlwidgets-1.5.3/htmlwidgets.js"></script>
<script src="slide_files/viz-1.8.2/viz.js"></script>
<link href="slide_files/DiagrammeR-styles-0.2/styles.css" rel="stylesheet" />
<script src="slide_files/grViz-binding-1.0.6.1/grViz.js"></script>
<script src="slide_files/jquery-3.5.1/jquery.min.js"></script>
<link href="slide_files/datatables-css-0.0.0/datatables-crosstalk.css" rel="stylesheet" />
<script src="slide_files/datatables-binding-0.17/datatables.js"></script>
<link href="slide_files/dt-core-1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<link href="slide_files/dt-core-1.10.20/css/jquery.dataTables.extra.css" rel="stylesheet" />
<script src="slide_files/dt-core-1.10.20/js/jquery.dataTables.min.js"></script>
<link href="slide_files/dt-ext-fixedcolumns-1.10.20/css/fixedColumns.dataTables.min.css" rel="stylesheet" />
<script src="slide_files/dt-ext-fixedcolumns-1.10.20/js/dataTables.fixedColumns.min.js"></script>
<link href="slide_files/crosstalk-1.1.1/css/crosstalk.css" rel="stylesheet" />
<script src="slide_files/crosstalk-1.1.1/js/crosstalk.min.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="title">初心者セッション</h1>
<h1 class="subtitle">R入門〜データハンドリング</h1>
<h2 class="author">y__mattu</h2>
<h3 class="date">2021/10/30 TokyoR #95</h3>
</section>
<section>
<section id="はじめに" class="title-slide slide level1">
<h1>はじめに</h1>
</section>
<section id="誰" class="slide level2">
<h2>誰?</h2>
<div class="column1">
<ul>
<li>松村優哉</li>
<li><u>Twitter</u>: <strong>y__mattu</strong></li>
<li>バックオフィスのクラウドサービスの会社で働くデータ屋さん</li>
<li>学生時代: 計量経済学、ベイズ統計、因果推論、マーケティング</li>
<li>R歴: 7年目</li>
<li><a href="https://ymattu.github.io/" class="uri">https://ymattu.github.io/</a></li>
<li><a href="http://y-mattu.hatenablog.com/" class="uri">http://y-mattu.hatenablog.com/</a></li>
<li>Tokyo.R 運営(初心者セッションとか)</li>
</ul>
</div>
<div class="column2">
<p><img data-src="slide_img/twitter_icon.jpg" alt="icon" /></p>
</div>
</section>
<section id="advertise" class="slide level2">
<h2>宣伝</h2>
<h4 id="改訂2版-r-ユーザのための-rstudio実践入門-tidyverse-によるモダンな分析フローの世界"><strong>改訂2版 R ユーザのための RStudio[実践]入門 <br> − tidyverse によるモダンな分析フローの世界−</strong></h4>
<div class="column1">
<p><img data-src="slide_img/rstudiobook2.jpg" alt="rstudiobook" /></p>
</section>
<section id="この資料の目的" class="slide level2">
<h2>この資料の目的</h2>
<ul>
<li>R 初心者(触ったことはあるけど、なんかよくわからない)が、雰囲気を掴む</li>
</ul>
</section>
<section id="contents" class="slide level2">
<h2>Contents</h2>
<ul>
<li>R と RStudio について</li>
<li>tidyverse について</li>
<li>テーブルデータの読み込み</li>
<li>データハンドリング</li>
<li>統計学・モデリング・可視化については触れません。</li>
</ul>
</section>
<section id="attention" class="slide level2">
<h2>注意</h2>
<ul>
<li>扱う範囲が広く資料の分量が多いので、特に重要なところをピックアップしながら進めます。</li>
<li>参考リンクも多いので資料は後でじっくり御覧ください。</li>
<li>パッケージ名だけでも覚えてかえっていただけると嬉しいです。</li>
</ul>
</section>
<section id="flow_of_analysis" class="slide level2">
<h2>データ分析の(おおまかな)流れ</h2>
<div id="htmlwidget-becb4a007e2604cb9e3c" style="width:600px;height:300px;" class="grViz html-widget"></div>
<script type="application/json" data-for="htmlwidget-becb4a007e2604cb9e3c">{"x":{"diagram":"\ndigraph data_analytics {\n # graph\n graph [overlap = true, fontsize = 10]\n # node\n node [shape=box,\n fontname = Helvetica,\n style = filled,\n fillcolor = SteelBlue,\n fontcolor = white]\n データ読み込み;可視化・モデリング; 前処理;\n # edge\n データ読み込み->前処理\n 前処理->可視化・モデリング\n }\n ","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]}</script>
</section></section>
<section>
<section id="r_and_rstudio" class="title-slide slide level1">
<h1>RとRStudioについて</h1>
</section>
<section id="aboutR" class="slide level2">
<h2>Rとは</h2>
<ul>
<li>統計解析およびその周辺環境に強いプログラミング言語
<ul>
<li>データの読み込み(ローカル, Webページ, DB)</li>
<li>データハンドリング</li>
<li>モデリング</li>
<li>可視化</li>
</ul></li>
<li>最近はWebアプリやAPI作成など、「プロダクションレベル」を支えられるような環境も整いつつある</li>
<li>また、機械学習やディープラーニングなど、これまでPythonに優位性があった手法も多くカバーされるようになってきた
<ul>
<li>tidymodels, keras, torchなど</li>
</ul></li>
<li>プログラミング未経験でも始めやすい(個人的な感想)</li>
</ul>
</section>
<section id="installR" class="slide level2">
<h2>R の環境構築</h2>
<ul>
<li>R のインストールは、<a href="https://www.r-project.org/">CRAN</a>から、自分のOSに合ったものを。</li>
<li>2021/10/30時点の最新版は、4.1.1
<ul>
<li>本日の講義内容に関しては、3.X.X以上であれば概ね問題ないと思われます</li>
</ul></li>
<li>3.X.X→4.X.Xの変更点
<ul>
<li><a href="https://qiita.com/piccolist/items/80f47a11d68dfe904cca">R 4.0.0 リリース - Qiita</a></li>
<li><a href="https://qiita.com/five-dots/items/6dc28ece9afa5b1b053c">R 4.0.0 の破壊的変更まとめ - Qiita</a></li>
</ul></li>
<li>おすすめのIDE(統合開発環境)は、<a href="https://rstudio.com/">RStudio</a></li>
</ul>
</section>
<section id="packages" class="slide level2">
<h2>R のパッケージ</h2>
<ul>
<li>R のパッケージを使うことで、世界中で開発されている便利な手法を使える</li>
<li>パッケージに含まれている関数を呼び出すことで、様々な拡張機能を使う</li>
<li>パッケージは、関数の集まり</li>
<li>CRANに登録されているものは、<code>install.packages("パッケージ名")</code> でインストール
<ul>
<li>例: <code>install.packages("ggplot2")</code></li>
</ul></li>
</ul>
</section>
<section id="パッケージ内の関数の表記" class="slide level2">
<h2>パッケージ内の関数の表記</h2>
<ul>
<li><strong>readr</strong> パッケージの read_csv 関数を使いたいとき</li>
</ul>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 方法 1</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">read_csv</span>(<span class="st">"hoge.csv"</span>)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"># 方法 2</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">"hoge.csv"</span>)</span></code></pre></div>
<ul>
<li>特に、「方法2」は関数が所属するパッケージを明示することで、あとから見たときにコードの確認がしやすかったり、他の人と共有する際に分かりやすいなど、利点が多く個人的にはおすすめ</li>
<li>ただし、本資料では直感的なわかりやすさを優先し「方法1」を多く使います</li>
</ul>
</section></section>
<section id="chapter_data_handling" class="title-slide slide level1">
<h1>データハンドリング編</h1>
</section>
<section>
<section id="tidyverse" class="title-slide slide level1">
<h1>tidyverse</h1>
</section>
<section id="abouttidyverse" class="slide level2">
<h2>tidyverse について</h2>
<h3 id="tidyverse概念">tidyverse(概念)</h3>
<p>ざっくり:</p>
<ul>
<li>R でやるいろんな操作(データハンドリング、可視化、スクレイピング、分析、etc)を直感的で統一的なインターフェースでできるようになったら嬉しくない?</li>
</ul>
<h3 id="tidyverse-パッケージ"><strong>tidyverse</strong> パッケージ</h3>
<ul>
<li>上記の概念を実現するためのコアパッケージ群</li>
<li><code>install.packages("tidyverse")</code>でインストール</li>
</ul>
</section>
<section id="library_tidyverse" class="slide level2">
<h2>tidyverse を読み込み</h2>
<div class="column1">
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code></pre></div>
<pre><code>── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──</code></pre>
<pre><code>✓ ggplot2 3.3.3 ✓ purrr 0.3.4
✓ tibble 3.1.0 ✓ dplyr 1.0.7
✓ tidyr 1.1.4 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.1</code></pre>
<pre><code>── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()</code></pre>
</div>
<div class="column2">
<p>読み込まれるパッケージ</p>
<ul>
<li>ggplot2: 可視化</li>
<li>dplyr: データの操作</li>
<li>tidyr: データを tidy に</li>
<li>readr: データの読み書き</li>
<li>purrr: 関数型プログラミング</li>
<li>stringr: 文字列の操作</li>
<li>forcats: 因子型データの操作</li>
<li>tibble: tibble というモダンなデータフレーム</li>
</ul>
</div>
</section></section>
<section>
<section id="データの読み込み" class="title-slide slide level1">
<h1>データの読み込み</h1>
</section>
<section id="bestpractice" class="slide level2">
<h2>R でのデータ読み込みのベストプラクティス</h2>
<ol start="0" type="1">
<li>RStudio でプロジェクトを作成
<ul>
<li>ファイルの位置が分かりやすくなります</li>
</ul></li>
<li>様々な読み込み関数を使って読み込み
<ul>
<li><strong>ローカルにあるファイル</strong>(今日の中心)</li>
<li><strong>データベース</strong>(パッケージの紹介のみ)</li>
<li>Web スクレイピング(またの機会に…)</li>
</ul></li>
</ol>
</section></section>
<section>
<section id="rstudio-でプロジェクトを作成" class="title-slide slide level1">
<h1>RStudio でプロジェクトを作成</h1>
</section>
<section id="project-new-project" class="slide level2">
<h2>Project → New Project</h2>
<p><img data-src="slide_img/project1.png" alt="project1" /></p>
</section>
<section id="newproj" class="slide level2">
<h2>New Directory → New Project</h2>
<div class="column1">
<p><img data-src="slide_img/newdir.png" alt="newdir" /></p>
</div>
<div class="column2">
<p><img data-src="slide_img/newproj.png" alt="newproj" /></p>
</div>
</section>
<section id="ディレクトリ名を入力" class="slide level2">
<h2>ディレクトリ名を入力</h2>
<p><img data-src="slide_img/dirname.png" alt="dirname" /></p>
</section>
<section id="done" class="slide level2">
<h2>Done!</h2>
<ul>
<li>読み込みの関数は、プロジェクトの中のファイルを探しにいきます。</li>
<li>書籍によっては <code>setwd()</code> を書いているものもありますが、RStudioプロジェクトでは必要ありません。
<ul>
<li>むしろ、RStudio プロジェクトでは非推奨</li>
</ul></li>
</ul>
</section></section>
<section id="いよいよデータの読み込み" class="title-slide slide level1">
<h1>いよいよデータの読み込み</h1>
</section>
<section id="ローカルにあるファイル" class="title-slide slide level1">
<h1>ローカルにあるファイル</h1>
</section>
<section>
<section id="csv" class="title-slide slide level1">
<h1>csv</h1>
</section>
<section id="read.csv" class="slide level2">
<h2><code>read.csv()</code></h2>
<ul>
<li>パッケージを使わない方法</li>
</ul>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"sample.csv"</span>)</span></code></pre></div>
<ul>
<li>R < 4.0.0 では <code>stringsAsFactors = TRUE</code> がデフォルトになっているので、<code>stringsAsFactors = FALSE</code> をつけることを推奨します。</li>
</ul>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"sample.csv"</span>, <span class="at">stringsAsFactors =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
</section>
<section id="readrread_csv" class="slide level2">
<h2><code>readr::read_csv()</code></h2>
<ul>
<li>高速で、列の型をいい感じにやってくれる(オススメ)</li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">"sample.csv"</span>)</span></code></pre></div>
</section>
<section id="data.tablefread" class="slide level2">
<h2><code>data.table::fread()</code></h2>
<ul>
<li><code>readr::read_csv()</code> よりも高速</li>
<li>デフォルトでは、data.table というデータフレームとは別の形で読み込まれるのでデータフレームがいいときは <code>data.table = FALSE</code></li>
</ul>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(data.table)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">fread</span>(<span class="st">"sample.csv"</span>, <span class="at">data.table =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
</section>
<section id="どれがいいのか" class="slide level2">
<h2>どれがいいのか</h2>
<ul>
<li>個人的には <strong>readr</strong>パッケージの <code>read_***()</code>関数が一番オススメ</li>
<li>速い、エンコーディングの調整が難しくない</li>
</ul>
<table>
<thead>
<tr class="header">
<th></th>
<th>read.***</th>
<th>read_***</th>
<th>fread</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>速さ(45MB)</td>
<td>3秒</td>
<td>0.8 秒</td>
<td>0.6秒</td>
</tr>
<tr class="even">
<td>区切り値の判定ミス</td>
<td>×</td>
<td>×</td>
<td>△</td>
</tr>
<tr class="odd">
<td>エンコーディング</td>
<td>○</td>
<td>○</td>
<td>△</td>
</tr>
</tbody>
</table>
</section></section>
<section>
<section id="xlsx-xls" class="title-slide slide level1">
<h1>xlsx, xls</h1>
</section>
<section id="エクセルファイル" class="slide level2">
<h2>エクセルファイル</h2>
<h3 id="エクセルファイルを読み込めるパッケージ">エクセルファイルを読み込めるパッケージ</h3>
<ul>
<li>xlsx</li>
<li>gdata</li>
<li>XLConnect</li>
<li>openxlsx</li>
<li><strong>readxl</strong> → オススメ(速い、列の型をいい感じに読める)</li>
</ul>
</section>
<section id="読み込み方" class="slide level2">
<h2>読み込み方</h2>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> readxl<span class="sc">::</span><span class="fu">read_excel</span>(<span class="st">"sample.xlsx"</span>, <span class="at">sheet =</span> <span class="st">"シート名"</span>)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="co"># シート名はシート番号でも OK</span></span></code></pre></div>
</section></section>
<section>
<section id="文字コードの指定" class="title-slide slide level1">
<h1>文字コードの指定</h1>
</section>
<section id="エンコーディング問題" class="slide level2">
<h2>エンコーディング問題</h2>
<ul>
<li>Windows の文字コードは <strong>Shift-JIS(CP932)</strong></li>
<li>Mac の文字コードは <strong>UTF8</strong></li>
<li>Windows で作られた(日本語を含む)ファイルを Mac で読むときは <code>Encoding=cp932</code></li>
<li>Mac で作られた(日本語を含む)ファイルを Windows で読むときは <code>Encoding=UTF8</code></li>
</ul>
</section>
<section id="csv-を-cp932-で読む" class="slide level2">
<h2>csv を CP932 で読む</h2>
<h3 id="r-の標準関数">R の標準関数</h3>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"sample.csv"</span>, <span class="at">stringAsFactors =</span> <span class="cn">FALSE</span>, <span class="at">fileEncoding =</span> <span class="st">"cp932"</span>)</span></code></pre></div>
<h3 id="readr">readr</h3>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">"sample.csv"</span>, <span class="at">locale =</span> <span class="fu">locale</span>(<span class="at">encoding =</span> <span class="st">"cp932"</span>))</span></code></pre></div>
<h3 id="data.table">data.table</h3>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>dat <span class="ot"><-</span> data.table<span class="sc">::</span><span class="fu">fread</span>(<span class="st">"sample.csv"</span>, <span class="at">data.table =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">VAR1 =</span> <span class="fu">iconv</span>(VAR1, <span class="at">from =</span> <span class="st">"UTF8"</span>, <span class="at">to =</span> <span class="st">"CP932"</span>))</span></code></pre></div>
</section></section>
<section>
<section id="関数とかオプション引数を-覚えられない" class="title-slide slide level1">
<h1>関数とかオプション(引数)を <br> 覚えられない</h1>
</section>
<section id="importdataset1" class="slide level2">
<h2>RStudio の GUI 読み込み</h2>
<p><img data-src="slide_img/dataimport.png" alt="dataimport" /></p>
</section>
<section id="importdataset2" class="slide level2">
<h2>RStudio の GUI 読み込み</h2>
<p><img data-src="slide_img/dataimport2.png" alt="dataimport2" /></p>
</section></section>
<section>
<section id="datahandling" class="title-slide slide level1">
<h1>データハンドリング</h1>
</section>
<section id="datahandling_todo" class="slide level2">
<h2>データハンドリングでやること、例えば</h2>
<ul>
<li>縦横変換</li>
<li>絞り込み(列・行)</li>
<li>新しい変数の作成</li>
<li>集計</li>
<li>テーブルのマージ</li>
<li>etc… →分析できる形に整形</li>
</ul>
</section>
<section id="datahandling_contents" class="slide level2">
<h2>データハンドリング編のコンテンツ</h2>
<ol start="0" type="1">
<li>パイプ演算子</li>
<li>tidy data</li>
<li>dplyr</li>
<li>FAQ</li>
</ol>
</section>
<section id="shuyaku" class="slide level2">
<h2>主役は</h2>
<div class="column1">
<p><img src="slide_img/dplyr1_logo.png" width="230"> <img data-src="slide_img/tidyr_logo.png" alt="tidyr" /></p>
</div>
<section id="特徴" class="column2">
<h4>特徴</h4>
<p>パッケージを使わないやり方より</p>
<ul>
<li>(大きいデータだと特に) <br> 速い</li>
<li>簡単 <br> ≒ わかりやすい</li>
<li>他の tidyverse のパッケージと相性がいい</li>
</ul>
</section>
</section>
<section id="datahandlinggoal" class="slide level2">
<h2>データハンドリング編のゴール</h2>
<ul>
<li>tidy data についてざっくり理解する</li>
<li>R の <strong>dplyr</strong> パッケージで簡単な集計ができるようになること</li>
<li>dplyr や他のパッケージで何ができるのかをなんとなく把握して、「ググり力」を身につける</li>
</ul>
</section></section>
<section>
<section id="pipe" class="title-slide slide level1">
<h1>%>%</h1>
</section>
<section id="pipe_operator" class="slide level2">
<h2>パイプ演算子</h2>
<ul>
<li>“これまでの処理を次の関数の第 1 引数として渡す」という働き”</li>
</ul>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span><span class="sc">:</span><span class="dv">3</span> <span class="sc">%>%</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">sum</span>()</span></code></pre></div>
<pre><code>[1] 6</code></pre>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># これと全く同じ</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>)</span></code></pre></div>
</section>
<section id="robot_example" class="slide level2">
<h2>例えば、以下の動作を考えてみる</h2>
<p><img data-src="slide_img/robot1.png" /></p>
</section>
<section id="howtowrite" class="slide level2">
<h2>どう書くのか問題</h2>
<p><img data-src="slide_img/robot2.png" /></p>
</section>
<section id="pipeline" class="slide level2">
<h2>思考の流れと書く流れ</h2>
<p><img data-src="slide_img/robot3.png" /></p>
</section>
<section id="hint_pipe" class="slide level2">
<h2>パイプ演算子を使うときのポイント</h2>
<ul>
<li><code>結果 <- スタート地点</code> を書いて、やりたい処理をパイプでつないでいく</li>
<li>RStudioでのキーボードショートカット
<ul>
<li>Windows: <code>Ctrl</code> + <code>Shift</code> + <code>M</code></li>
<li>Mac: <code>Cmd</code> + <code>Shift</code> + <code>M</code></li>
</ul></li>
</ul>
</section></section>
<section>
<section id="tidydata" class="title-slide slide level1">
<h1>tidy data</h1>
</section>
<section id="data_format" class="slide level2">
<h2>データの形式</h2>
<p>2つのデータ形式(例: カテゴリごとの購買金額(千円))</p>