Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
731 lines (650 loc) · 17.8 KB
/
Copy pathindex.html
File metadata and controls
731 lines (650 loc) · 17.8 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
<!DOCTYPE html>
<html>
<head>
<title>Using data.table</title>
<meta charset="utf-8">
<meta name="description" content="Using data.table">
<meta name="author" content="Jeffrey Leek">
<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="../../librariesNew/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="../../librariesNew/frameworks/io2012/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="../../librariesNew/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="../../librariesNew/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="../../librariesNew/widgets/quiz/js/jquery.js"><\/script>')</script>
<script data-main="../../librariesNew/frameworks/io2012/js/slides"
src="../../librariesNew/frameworks/io2012/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">
<aside class="gdbar">
<img src="../../assets/img/bloomberg_shield.png">
</aside>
<hgroup class="auto-fadein">
<h1>Using data.table</h1>
<h2></h2>
<p>Jeffrey Leek<br/>Johns Hopkins Bloomberg School of Public Health</p>
</hgroup>
<article></article>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>data.table</h2>
</hgroup>
<article data-timings="">
<ul>
<li>Inherets from data.frame
<ul>
<li>All functions that accept data.frame work on data.table</li>
</ul></li>
<li>Written in C so it is much faster</li>
<li>Much, much faster at subsetting, group, and updating</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>Create data tables just like data frames</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">library(data.table)
DF = data.frame(x=rnorm(9),y=rep(c("a","b","c"),each=3),z=rnorm(9))
head(DF,3)
</code></pre>
<pre><code> x y z
1 0.8528 a -0.97110
2 1.1736 a -0.69167
3 -0.7739 a 0.06864
</code></pre>
<pre><code class="r">DT = data.table(x=rnorm(9),y=rep(c("a","b","c"),each=3),z=rnorm(9))
head(DT,3)
</code></pre>
<pre><code> x y z
1: 0.41032 a 0.10602
2: -1.74969 a -0.65800
3: -0.08614 a 0.05627
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>See all the data tables in memory</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">tables()
</code></pre>
<pre><code> NAME NROW MB COLS KEY
[1,] DT 9 1 x,y,z
[2,] DT1 4 1 x,y x
[3,] DT2 3 1 x,z x
Total: 3MB
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>Subsetting rows</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[2,]
</code></pre>
<pre><code> x y z
1: -1.75 a -0.658
</code></pre>
<pre><code class="r">DT[DT$y=="a",]
</code></pre>
<pre><code> x y z
1: 0.41032 a 0.10602
2: -1.74969 a -0.65800
3: -0.08614 a 0.05627
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>Subsetting rows</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[c(2,3)]
</code></pre>
<pre><code> x y z
1: -1.74969 a -0.65800
2: -0.08614 a 0.05627
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>Subsetting columns!?</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,c(2,3)]
</code></pre>
<pre><code>[1] 2 3
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>Column subsetting in data.table</h2>
</hgroup>
<article data-timings="">
<ul>
<li>The subsetting function is modified for data.table</li>
<li>The argument you pass after the comma is called an "expression"</li>
<li>In R an expression is a collection of statements enclosed in curley brackets </li>
</ul>
<pre><code class="r">{
x = 1
y = 2
}
k = {print(10); 5}
</code></pre>
<pre><code>[1] 10
</code></pre>
<pre><code class="r">print(k)
</code></pre>
<pre><code>[1] 5
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background:;">
<hgroup>
<h2>Calculating values for variables with expressions</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,list(mean(x),sum(z))]
</code></pre>
<pre><code> V1 V2
1: 0.477 -3.307
</code></pre>
<pre><code class="r">DT[,table(y)]
</code></pre>
<pre><code>y
a b c
3 3 3
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>Adding new columns</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,w:=z^2]
</code></pre>
<pre><code> x y z w
1: 0.41032 a 0.10602 0.011239
2: -1.74969 a -0.65800 0.432964
3: -0.08614 a 0.05627 0.003167
4: 2.17432 b 0.44756 0.200306
5: 1.98301 b -0.63096 0.398111
6: 0.10673 b 0.10226 0.010458
7: 0.20678 c -0.69943 0.489205
8: -0.95958 c -0.78335 0.613640
9: 2.20682 c -1.24722 1.555565
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10" style="background:;">
<hgroup>
<h2>Adding new columns</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT2 <- DT
DT[, y:= 2]
</code></pre>
<pre><code> x y z w
1: 0.41032 2 0.10602 0.011239
2: -1.74969 2 -0.65800 0.432964
3: -0.08614 2 0.05627 0.003167
4: 2.17432 2 0.44756 0.200306
5: 1.98301 2 -0.63096 0.398111
6: 0.10673 2 0.10226 0.010458
7: 0.20678 2 -0.69943 0.489205
8: -0.95958 2 -0.78335 0.613640
9: 2.20682 2 -1.24722 1.555565
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11" style="background:;">
<hgroup>
<h2>Careful</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">head(DT,n=3)
</code></pre>
<pre><code> x y z w
1: 0.41032 2 0.10602 0.011239
2: -1.74969 2 -0.65800 0.432964
3: -0.08614 2 0.05627 0.003167
</code></pre>
<pre><code class="r">head(DT2,n=3)
</code></pre>
<pre><code> x y z w
1: 0.41032 2 0.10602 0.011239
2: -1.74969 2 -0.65800 0.432964
3: -0.08614 2 0.05627 0.003167
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h2>Multiple operations</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,m:= {tmp <- (x+z); log2(tmp+5)}]
</code></pre>
<pre><code> x y z w m
1: 0.41032 2 0.10602 0.011239 2.464
2: -1.74969 2 -0.65800 0.432964 1.374
3: -0.08614 2 0.05627 0.003167 2.313
4: 2.17432 2 0.44756 0.200306 2.930
5: 1.98301 2 -0.63096 0.398111 2.667
6: 0.10673 2 0.10226 0.010458 2.381
7: 0.20678 2 -0.69943 0.489205 2.172
8: -0.95958 2 -0.78335 0.613640 1.704
9: 2.20682 2 -1.24722 1.555565 2.575
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h2>plyr like operations</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,a:=x>0]
</code></pre>
<pre><code> x y z w m a
1: 0.41032 2 0.10602 0.011239 2.464 TRUE
2: -1.74969 2 -0.65800 0.432964 1.374 FALSE
3: -0.08614 2 0.05627 0.003167 2.313 FALSE
4: 2.17432 2 0.44756 0.200306 2.930 TRUE
5: 1.98301 2 -0.63096 0.398111 2.667 TRUE
6: 0.10673 2 0.10226 0.010458 2.381 TRUE
7: 0.20678 2 -0.69943 0.489205 2.172 TRUE
8: -0.95958 2 -0.78335 0.613640 1.704 FALSE
9: 2.20682 2 -1.24722 1.555565 2.575 TRUE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<hgroup>
<h2>plyr like operations</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT[,b:= mean(x+w),by=a]
</code></pre>
<pre><code> x y z w m a b
1: 0.41032 2 0.10602 0.011239 2.464 TRUE 1.6255
2: -1.74969 2 -0.65800 0.432964 1.374 FALSE -0.5819
3: -0.08614 2 0.05627 0.003167 2.313 FALSE -0.5819
4: 2.17432 2 0.44756 0.200306 2.930 TRUE 1.6255
5: 1.98301 2 -0.63096 0.398111 2.667 TRUE 1.6255
6: 0.10673 2 0.10226 0.010458 2.381 TRUE 1.6255
7: 0.20678 2 -0.69943 0.489205 2.172 TRUE 1.6255
8: -0.95958 2 -0.78335 0.613640 1.704 FALSE -0.5819
9: 2.20682 2 -1.24722 1.555565 2.575 TRUE 1.6255
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background:;">
<hgroup>
<h2>Special variables</h2>
</hgroup>
<article data-timings="">
<p><code>.N</code> An integer, length 1, containing the number of elements of a factor level </p>
<pre><code class="r">set.seed(123);
DT <- data.table(x=sample(letters[1:3], 1E5, TRUE))
DT[, .N, by=x]
</code></pre>
<pre><code> x N
1: a 33387
2: c 33201
3: b 33412
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h2>Keys</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT <- data.table(x=rep(c("a","b","c"),each=100), y=rnorm(300))
setkey(DT, x)
DT['a']
</code></pre>
<pre><code> x y
1: a 0.25959
2: a 0.91751
3: a -0.72232
4: a -0.80828
5: a -0.14135
6: a 2.25701
7: a -2.37955
8: a -0.45425
9: a -0.06007
10: a 0.86090
11: a -1.78466
12: a -0.13074
13: a -0.36984
14: a -0.18066
15: a -1.04973
16: a 0.37832
17: a -1.37079
18: a -0.31612
19: a 0.39435
20: a -1.68988
21: a -1.46234
22: a 2.55838
23: a 0.08789
24: a 1.73141
25: a 1.21513
26: a 0.29954
27: a -0.17246
28: a 1.13250
29: a 0.02320
30: a 1.33587
31: a -1.09879
32: a -0.58176
33: a 0.03892
34: a 1.07315
35: a 1.34970
36: a 1.19528
37: a -0.02218
38: a 0.69849
39: a 0.67241
40: a -0.79165
41: a -0.21791
42: a 0.02307
43: a 0.11539
44: a -0.27708
45: a 0.03688
46: a 0.47520
47: a 1.70749
48: a 1.07601
49: a -1.34571
50: a -1.44025
51: a -0.39393
52: a 0.58106
53: a -0.17079
54: a -0.90585
55: a 0.15621
56: a -0.37323
57: a -0.34587
58: a -0.35829
59: a -0.13307
60: a -0.08960
61: a 0.62793
62: a -1.42883
63: a 0.17255
64: a -0.79115
65: a 1.26204
66: a -0.26941
67: a 0.15698
68: a -0.76060
69: a 1.37060
70: a 0.03758
71: a 0.44949
72: a 2.78869
73: a -0.46849
74: a 1.01261
75: a -0.04374
76: a 1.40670
77: a 0.41993
78: a 0.31009
79: a 1.11905
80: a -1.29814
81: a -1.28248
82: a 1.65943
83: a 0.78375
84: a 0.57771
85: a -0.26725
86: a -0.64569
87: a -0.44953
88: a -0.82620
89: a 1.05504
90: a -0.87927
91: a -1.27713
92: a -0.63412
93: a 0.66470
94: a -0.50958
95: a 0.40736
96: a 1.67775
97: a -1.05206
98: a -0.63691
99: a 0.56539
100: a 0.38016
x y
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background:;">
<hgroup>
<h2>Joins</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">DT1 <- data.table(x=c('a', 'a', 'b', 'dt1'), y=1:4)
DT2 <- data.table(x=c('a', 'b', 'dt2'), z=5:7)
setkey(DT1, x); setkey(DT2, x)
merge(DT1, DT2)
</code></pre>
<pre><code> x y z
1: a 1 5
2: a 2 5
3: b 3 6
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h2>Fast reading</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">big_df <- data.frame(x=rnorm(1E6), y=rnorm(1E6))
file <- tempfile()
write.table(big_df, file=file, row.names=FALSE, col.names=TRUE, sep="\t", quote=FALSE)
system.time(fread(file))
</code></pre>
<pre><code> user system elapsed
0.312 0.015 0.326
</code></pre>
<pre><code class="r">system.time(read.table(file, header=TRUE, sep="\t"))
</code></pre>
<pre><code> user system elapsed
5.702 0.048 5.755
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h2>Summary and further reading</h2>
</hgroup>
<article data-timings="">
<ul>
<li>The latest development version contains new functions like <code>melt</code> and <code>dcast</code> for data.tables
<ul>
<li><a href="https://r-forge.r-project.org/scm/viewvc.php/pkg/NEWS?view=markup&root=datatable">https://r-forge.r-project.org/scm/viewvc.php/pkg/NEWS?view=markup&root=datatable</a></li>
</ul></li>
<li>Here is a list of differences between data.table and data.frame
<ul>
<li><a href="http://stackoverflow.com/questions/13618488/what-you-can-do-with-data-frame-that-you-cant-in-data-table">http://stackoverflow.com/questions/13618488/what-you-can-do-with-data-frame-that-you-cant-in-data-table</a></li>
</ul></li>
<li>Notes based on Raphael Gottardo's notes <a href="https://github.com/raphg/Biostat-578/blob/master/Advanced_data_manipulation.Rpres">https://github.com/raphg/Biostat-578/blob/master/Advanced_data_manipulation.Rpres</a>, who got them from Kevin Ushey.</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="backdrop"></slide>
</slides>
<div class="pagination pagination-small" id='io2012-ptoc' style="display:none;">
<ul>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=1 title='data.table'>
1
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=2 title='Create data tables just like data frames'>
2
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=3 title='See all the data tables in memory'>
3
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=4 title='Subsetting rows'>
4
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=5 title='Subsetting rows'>
5
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=6 title='Subsetting columns!?'>
6
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=7 title='Column subsetting in data.table'>
7
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=8 title='Calculating values for variables with expressions'>
8
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=9 title='Adding new columns'>
9
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=10 title='Adding new columns'>
10
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=11 title='Careful'>
11
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=12 title='Multiple operations'>
12
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=13 title='plyr like operations'>
13
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=14 title='plyr like operations'>
14
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=15 title='Special variables'>
15
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=16 title='Keys'>
16
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=17 title='Joins'>
17
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=18 title='Fast reading'>
18
</a>
</li>
<li>
<a href="#" target="_self" rel='tooltip'
data-slide=19 title='Summary and further reading'>
19
</a>
</li>
</ul>
</div> <!--[if IE]>
<script
src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js">
</script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
<!-- Load Javascripts for Widgets -->
<!-- MathJax: Fall back to local if CDN offline but local image fonts are not supported (saves >100MB) -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script> -->
<script>window.MathJax || document.write('<script type="text/x-mathjax-config">MathJax.Hub.Config({"HTML-CSS":{imageFont:null}});<\/script><script src="../../librariesNew/widgets/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"><\/script>')
</script>
<!-- LOAD HIGHLIGHTER JS FILES -->
<script src="../../librariesNew/highlighters/highlight.js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- DONE LOADING HIGHLIGHTER JS FILES -->
</html>