-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslides_03.html
1864 lines (1458 loc) · 52.4 KB
/
slides_03.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 lang="" xml:lang="">
<head>
<title>Sesión II: Manipulando datos</title>
<meta charset="utf-8" />
<meta name="author" content="Guillermo de Anda-Jáuregui y Laura Gómez-Romero" />
<meta name="date" content="2022-01-01" />
<script src="libs/header-attrs/header-attrs.js"></script>
<link href="libs/remark-css/default.css" rel="stylesheet" />
<link href="libs/remark-css/default-fonts.css" rel="stylesheet" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Sesión II: Manipulando datos
### Guillermo de Anda-Jáuregui y Laura Gómez-Romero
### 2022
---
class: inverse, center
# ¿Qué es tener datos **limpios**?
---
class: inverse, center, middle
<img src="figures/untidy_data.png">
---
class: inverse, center, middle
# Los datos limpios
## - Cada columna es una variable
## - Cada renglón es una observación
---
class: inverse, center, middle
<img src="figures/tidy_data.png">
---
class: inverse, center, middle
```r
my_data
```
```
## # A tibble: 7 x 10
## ID direccion subdireccion puesto apellido_paterno apellido_materno nombre
## <dbl> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 1 administr… limpieza limpie… Sanchez Pérez Santi…
## 2 2 jurídico legal practi… Lopez Domínguez Luisa
## 3 3 jurídico legal practi… Romero Madero Agust…
## 4 4 operativo ventas ventas Jimenez Ordoñez Luis
## 5 5 administr… transporte chofer Perez Rubio Diana
## 6 6 administr… finanzas analis… García Moreno Danie…
## 7 7 operativo compras compras Cabrera NA Miguel
## # … with 3 more variables: fecha_inicio <chr>, fecha_fin <chr>, interno <chr>
```
---
class: inverse, center
# El paquete tidyR
<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/tidyr.png", height = "90">
---
class: inverse, center
# Datos limpios, código limpio
.left[
### - Si tenemos datos limpios, podemos pensar en usar código parecido para diferentes problemas.
### - Código limpio: más legible.
### - Más tiempo para pensar en nuestros análisis.
]
---
class: inverse, center
# El paquete magrittR
<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/pipe.png", height = "90">
---
class: inverse, center
# Funciones anidadas
```r
x <- 1:10
(exp(sqrt(log(x))))
```
```
## [1] 1.000000 2.299185 2.852361 3.245956 3.556000 3.813572 4.034808 4.229259
## [9] 4.403078 4.560477
```
---
class: inverse, center
# Objetos intermedios
```r
x <- 1:10
y <- log(x)
z <- sqrt(y)
w <- exp(z)
w
```
```
## [1] 1.000000 2.299185 2.852361 3.245956 3.556000 3.813572 4.034808 4.229259
## [9] 4.403078 4.560477
```
---
class: inverse, center
# Sobreescribir mi objeto
```r
x <- 1:10
x <- log(x)
x <- sqrt(x)
x <- exp(x)
x
```
```
## [1] 1.000000 2.299185 2.852361 3.245956 3.556000 3.813572 4.034808 4.229259
## [9] 4.403078 4.560477
```
---
class: inverse, center
# El pipe
# %>%
---
class: inverse, center
# El pipe %>%
```r
x <- 1:10
my_result <-
x %>%
log() %>%
sqrt() %>%
exp
my_result
```
```
## [1] 1.000000 2.299185 2.852361 3.245956 3.556000 3.813572 4.034808 4.229259
## [9] 4.403078 4.560477
```
---
class: inverse, center
# Otros pipes
## la TE
```r
rnorm(100) %>%
matrix(ncol = 2) %T>%
plot() %>%
str()
```
![](slides_03_files/figure-html/unnamed-chunk-8-1.png)<!-- -->
```
## num [1:50, 1:2] 0.286 -0.859 -0.125 -0.58 1.793 ...
```
---
class: inverse, center
# Otros pipes
## la EXPLOSIÓN
```r
mtcars %$%
cor(disp, mpg)
```
```
## [1] -0.8475514
```
---
class: inverse, center
# ¿Qué queremos manipular?
.left[
## En un set de datos rectangulares...
### - Queremos modificar variables completas.
### - Queremos hacer operaciones y sacar nuevas variables.
### - Queremos seleccionar observaciones que cumplan características.
### - Queremos agrupar.
### - Queremos sacar descripciones de los datos.
### - Queremos reordenar variables.
]
---
class: inverse, center
# Dataset: nycflights13
<img src ="figures/nyc_plane.jpg">
https://thumbs.dreamstime.com/b/airplane-take-off-new-york-skyline-travel-concept-64524867.jpg
---
class: inverse, center
# Filtrados
# El paquete dplyr
<img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/dplyr.png", height = "90">
--
## Una *gramática* para la manipulación de datos
---
class: inverse, center, middle
# Tidyverse => Verbos
--
### *mutate*
### *select*
### *filter*
### *group_by*
### *summarise*
### *arrange*
---
class: inverse, center, middle
#Analicemos flights
---
class: inverse, center, middle
```r
flights <- vroom::vroom(file = "data/flights.txt")
```
```
## Rows: 336776 Columns: 19
```
```
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (4): carrier, tailnum, origin, dest
## dbl (14): year, month, day, dep_time, sched_dep_time, dep_delay, arr_time, ...
## dttm (1): time_hour
```
```
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
```
---
class: inverse, center, middle
```r
flights
```
```
## # A tibble: 336,776 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # … with 336,766 more rows, and 11 more variables: arr_delay <dbl>,
## # carrier <chr>, flight <dbl>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# Filtrados
### Buscamos observaciones (renglones) que cumplan una condición lógica
# Verbo *filter*
---
class: inverse, center
# filter
```r
filter(.data = flights,
dest == "SBN")
```
```
## # A tibble: 10 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 10 18 1820 1745 35 2030 2011
## 2 2013 11 1 2012 1905 67 2221 2131
## 3 2013 11 22 2013 1905 68 2224 2131
## 4 2013 12 1 1241 1215 26 1431 1431
## 5 2013 8 30 1909 1910 -1 2117 2136
## 6 2013 9 1 833 840 -7 1030 1040
## 7 2013 9 8 847 840 7 1043 1040
## 8 2013 9 20 1948 1950 -2 2207 2216
## 9 2013 9 22 837 840 -3 1025 1040
## 10 2013 9 27 2011 1950 21 2209 2216
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <dbl>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# filter
```r
flights %>%
filter(dest == "HDN")
```
```
## # A tibble: 15 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 5 829 830 -1 1047 1111
## 2 2013 1 12 827 830 -3 1112 1111
## 3 2013 1 19 843 830 13 1123 1111
## 4 2013 1 26 828 830 -2 1114 1111
## 5 2013 12 21 916 830 46 1149 1117
## 6 2013 12 28 913 829 44 1128 1116
## 7 2013 2 2 858 830 28 1124 1111
## 8 2013 2 9 NA 830 NA NA 1111
## 9 2013 2 16 834 830 4 1114 1111
## 10 2013 2 23 826 830 -4 1050 1111
## 11 2013 3 2 854 830 24 1104 1111
## 12 2013 3 9 838 830 8 1107 1111
## 13 2013 3 16 845 830 15 1154 1111
## 14 2013 3 23 835 830 5 1104 1111
## 15 2013 3 30 825 830 -5 1045 1111
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <dbl>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# filter
```r
flights %>%
filter(dest == "HDN",
!is.na(dep_time)
)
```
```
## # A tibble: 14 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 5 829 830 -1 1047 1111
## 2 2013 1 12 827 830 -3 1112 1111
## 3 2013 1 19 843 830 13 1123 1111
## 4 2013 1 26 828 830 -2 1114 1111
## 5 2013 12 21 916 830 46 1149 1117
## 6 2013 12 28 913 829 44 1128 1116
## 7 2013 2 2 858 830 28 1124 1111
## 8 2013 2 16 834 830 4 1114 1111
## 9 2013 2 23 826 830 -4 1050 1111
## 10 2013 3 2 854 830 24 1104 1111
## 11 2013 3 9 838 830 8 1107 1111
## 12 2013 3 16 845 830 15 1154 1111
## 13 2013 3 23 835 830 5 1104 1111
## 14 2013 3 30 825 830 -5 1045 1111
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <dbl>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# slice
### Nos deja seleccionar renglones por posición
```r
flights %>%
slice(14:17)
```
```
## # A tibble: 4 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 1 558 600 -2 923 937
## 2 2013 1 1 559 600 -1 941 910
## 3 2013 1 1 559 559 0 702 706
## 4 2013 1 1 559 600 -1 854 902
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <dbl>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# select
## Permite escoger algunas variables (columnas)
---
class: inverse, center
# select
```r
flights %>%
select(flight, carrier, origin, dest, distance)
```
```
## # A tibble: 336,776 x 5
## flight carrier origin dest distance
## <dbl> <chr> <chr> <chr> <dbl>
## 1 1545 UA EWR IAH 1400
## 2 1714 UA LGA IAH 1416
## 3 1141 AA JFK MIA 1089
## 4 725 B6 JFK BQN 1576
## 5 461 DL LGA ATL 762
## 6 1696 UA EWR ORD 719
## 7 507 B6 EWR FLL 1065
## 8 5708 EV LGA IAD 229
## 9 79 B6 JFK MCO 944
## 10 301 AA LGA ORD 733
## # … with 336,766 more rows
```
---
class: inverse, center
```r
flights %>%
select(contains("time"))
```
```
## # A tibble: 336,776 x 6
## dep_time sched_dep_time arr_time sched_arr_time air_time time_hour
## <dbl> <dbl> <dbl> <dbl> <dbl> <dttm>
## 1 517 515 830 819 227 2013-01-01 10:00:00
## 2 533 529 850 830 227 2013-01-01 10:00:00
## 3 542 540 923 850 160 2013-01-01 10:00:00
## 4 544 545 1004 1022 183 2013-01-01 10:00:00
## 5 554 600 812 837 116 2013-01-01 11:00:00
## 6 554 558 740 728 150 2013-01-01 10:00:00
## 7 555 600 913 854 158 2013-01-01 11:00:00
## 8 557 600 709 723 53 2013-01-01 11:00:00
## 9 557 600 838 846 140 2013-01-01 11:00:00
## 10 558 600 753 745 138 2013-01-01 11:00:00
## # … with 336,766 more rows
```
---
class: inverse, center
```r
flights %>%
select_if(is_character)
```
```
## # A tibble: 336,776 x 4
## carrier tailnum origin dest
## <chr> <chr> <chr> <chr>
## 1 UA N14228 EWR IAH
## 2 UA N24211 LGA IAH
## 3 AA N619AA JFK MIA
## 4 B6 N804JB JFK BQN
## 5 DL N668DN LGA ATL
## 6 UA N39463 EWR ORD
## 7 B6 N516JB EWR FLL
## 8 EV N829AS LGA IAD
## 9 B6 N593JB JFK MCO
## 10 AA N3ALAA LGA ORD
## # … with 336,766 more rows
```
---
class: inverse, center
#Podemos concatenar verbos usando el pipe
```r
flights %>%
select("dest", contains("time")) %>%
filter(dest == "HDN") %>%
tidyr::drop_na() %>%
slice(1:7)
```
```
## # A tibble: 7 x 7
## dest dep_time sched_dep_time arr_time sched_arr_time air_time
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 HDN 829 830 1047 1111 243
## 2 HDN 827 830 1112 1111 259
## 3 HDN 843 830 1123 1111 260
## 4 HDN 828 830 1114 1111 265
## 5 HDN 916 830 1149 1117 254
## 6 HDN 913 829 1128 1116 239
## 7 HDN 858 830 1124 1111 246
## # … with 1 more variable: time_hour <dttm>
```
---
class: inverse, center
# pull
### Permite sacar una variable como vector
```r
flights %>%
pull("dest") %>%
head
```
```
## [1] "IAH" "IAH" "MIA" "BQN" "ATL" "ORD"
```
---
class: inverse, center
# Agrupamientos
### *group_by* me permite agregar por variables categóricas (o coercible a variables categóricas).
### Esto me servirá para hacer resúmenes después.
---
class: inverse, center
# Agrupamientos
```r
flights %>%
group_by(dest)
```
```
## # A tibble: 336,776 x 19
## # Groups: dest [105]
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # … with 336,766 more rows, and 11 more variables: arr_delay <dbl>,
## # carrier <chr>, flight <dbl>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# Agrupamientos
```r
flights %>%
group_by(dest, origin)
```
```
## # A tibble: 336,776 x 19
## # Groups: dest, origin [224]
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## 7 2013 1 1 555 600 -5 913 854
## 8 2013 1 1 557 600 -3 709 723
## 9 2013 1 1 557 600 -3 838 846
## 10 2013 1 1 558 600 -2 753 745
## # … with 336,766 more rows, and 11 more variables: arr_delay <dbl>,
## # carrier <chr>, flight <dbl>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
```
---
class: inverse, center
# Funciones de resumen
### Nos van a dar descripciones de nuestro conjunto de datos
### Funcionan en el conjunto original, filtrado, agrupado, o manipulado
---
class: inverse, center
```r
flights %>%
summarise(mean_distance = mean(distance))
```
```
## # A tibble: 1 x 1
## mean_distance
## <dbl>
## 1 1040.
```
---
class: inverse, center
```r
flights %>%
select(contains("time")) %>%
summarise(mean_deptime = mean(dep_time),
mean_sched_deptime = mean(sched_dep_time),
mean_arr_time = mean(arr_time),
mean_sched_arr_time = mean(sched_arr_time),
mean_air_time = mean(air_time),
mean_time_hour = mean(time_hour)
)
```
```
## # A tibble: 1 x 6
## mean_deptime mean_sched_deptime mean_arr_time mean_sched_arr_ti… mean_air_time
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NA 1344. NA 1536. NA
## # … with 1 more variable: mean_time_hour <dttm>
```
---
class: inverse, center
```r
flights %>%
select(contains("time")) %>%
summarise_all(mean, na.rm=TRUE)
```
```
## # A tibble: 1 x 6
## dep_time sched_dep_time arr_time sched_arr_time air_time time_hour
## <dbl> <dbl> <dbl> <dbl> <dbl> <dttm>
## 1 1349. 1344. 1502. 1536. 151. 2013-07-03 09:22:54
```
---
class: inverse, center
# Puedo usarlas sobre datos agrupados
```r
flights %>%
group_by(dest) %>%
select(contains("time")) %>%
summarise_all(mean, na.rm=TRUE)
```
```
## Adding missing grouping variables: `dest`
```
```
## # A tibble: 105 x 7
## dest dep_time sched_dep_time arr_time sched_arr_time air_time
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ABQ 2006. 2001. 2049. 2278. 249.
## 2 ACK 1033. 1033. 1145. 1139. 42.1
## 3 ALB 1627. 1610. 1702. 1718. 31.8
## 4 ANC 1635. 1618. 1968 1966. 413.
## 5 ATL 1293. 1287. 1513. 1529. 113.
## 6 AUS 1521. 1506. 1614. 1816. 213.
## 7 AVL 1175. 1169. 1373. 1362. 89.9
## 8 BDL 1490. 1506. 1549. 1610. 25.5
## 9 BGR 1690. 1673. 1715. 1836. 54.1
## 10 BHM 1944. 1909. 2028. 2078. 123.
## # … with 95 more rows, and 1 more variable: time_hour <dttm>
```
---
class: inverse, center
# Puedo usarlas sobre datos agrupados
```r
flights %>%
group_by(origin, dest) %>%
select(contains("time")) %>%
summarise_all(list(med = median,
avg = mean
),
na.rm = TRUE
)
```
```
## Adding missing grouping variables: `origin`, `dest`
```
```
## # A tibble: 224 x 14
## # Groups: origin [3]
## origin dest dep_time_med sched_dep_time_med arr_time_med sched_arr_time_med
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 EWR ALB 1738 1721 1832 1822
## 2 EWR ANC 1618 1615 1957 1953
## 3 EWR ATL 1257 1300 1513 1519
## 4 EWR AUS 1503 1459 1746 1805
## 5 EWR AVL 1156 1200 1339 1350
## 6 EWR BDL 1328. 1327 1414. 1422
## 7 EWR BNA 1306. 1300 1421 1425
## 8 EWR BOS 1400 1400 1458 1511
## 9 EWR BQN 2054 2038 105 42
## 10 EWR BTV 1231 1209 1316 1320
## # … with 214 more rows, and 8 more variables: air_time_med <dbl>,
## # time_hour_med <dttm>, dep_time_avg <dbl>, sched_dep_time_avg <dbl>,
## # arr_time_avg <dbl>, sched_arr_time_avg <dbl>, air_time_avg <dbl>,
## # time_hour_avg <dttm>
```
---
class: inverse, center
# Contar: n, count, tally
### n
```r
flights %>%
group_by(month, origin, dest) %>%
summarize(cuantos = n())
```
```
## `summarise()` has grouped output by 'month', 'origin'. You can override using the `.groups` argument.
```
```
## # A tibble: 2,313 x 4
## # Groups: month, origin [36]
## month origin dest cuantos
## <dbl> <chr> <chr> <int>
## 1 1 EWR ALB 64
## 2 1 EWR ATL 362
## 3 1 EWR AUS 51
## 4 1 EWR AVL 2
## 5 1 EWR BDL 37
## 6 1 EWR BNA 111
## 7 1 EWR BOS 430
## 8 1 EWR BQN 31
## 9 1 EWR BTV 100
## 10 1 EWR BUF 119
## # … with 2,303 more rows
```
---
class: inverse, center
### count
```r
flights %>%
count()
```
```
## # A tibble: 1 x 1
## n
## <int>
## 1 336776
```
---
class: inverse, center
### count
```r
flights %>%
count(origin)
```
```
## # A tibble: 3 x 2
## origin n
## <chr> <int>
## 1 EWR 120835
## 2 JFK 111279
## 3 LGA 104662
```
---
class: inverse, center
### count - datos agrupados
```r
flights %>%
group_by(month) %>%
count(origin)
```
```
## # A tibble: 36 x 3
## # Groups: month [12]
## month origin n
## <dbl> <chr> <int>
## 1 1 EWR 9893
## 2 1 JFK 9161
## 3 1 LGA 7950
## 4 2 EWR 9107
## 5 2 JFK 8421
## 6 2 LGA 7423
## 7 3 EWR 10420
## 8 3 JFK 9697
## 9 3 LGA 8717
## 10 4 EWR 10531
## # … with 26 more rows
```
---
class: inverse, center
#tally
```r
flights %>%
group_by(month, origin, dest) %>%
tally()
```
```
## # A tibble: 2,313 x 4
## # Groups: month, origin [36]
## month origin dest n
## <dbl> <chr> <chr> <int>
## 1 1 EWR ALB 64
## 2 1 EWR ATL 362
## 3 1 EWR AUS 51
## 4 1 EWR AVL 2
## 5 1 EWR BDL 37
## 6 1 EWR BNA 111
## 7 1 EWR BOS 430
## 8 1 EWR BQN 31
## 9 1 EWR BTV 100
## 10 1 EWR BUF 119
## # … with 2,303 more rows
```
---
class: inverse, center
# Rangos y disperciones
```r
flights %>%
group_by(origin, dest) %>%
summarise(min_airtime = min(air_time, na.rm = TRUE),
max_airtime = max(air_time, na.rm = TRUE),
std_dev_airtime = sd(air_time, na.rm = TRUE),
cuantos = n()
)
```
```
## Warning in min(air_time, na.rm = TRUE): no non-missing arguments to min;
## returning Inf
```
```
## Warning in max(air_time, na.rm = TRUE): no non-missing arguments to max;
## returning -Inf
```
```
## `summarise()` has grouped output by 'origin'. You can override using the `.groups` argument.
```
```
## # A tibble: 224 x 6
## # Groups: origin [3]
## origin dest min_airtime max_airtime std_dev_airtime cuantos
## <chr> <chr> <dbl> <dbl> <dbl> <int>
## 1 EWR ALB 24 50 3.08 439
## 2 EWR ANC 388 434 14.7 8
## 3 EWR ATL 88 176 9.99 5022
## 4 EWR AUS 174 301 17.9 968
## 5 EWR AVL 76 119 7.45 265