library(nycflights13)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.2.1     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(dplyr)

data(flights)

Aplicando funciones como min, max, mean, sum, count, etc con group_by() y summarize()

#16 Cuál es la media del retraso en los vuelos, agrupa por year, month, day
#Elimina los Na's
#Identifica el día, mes, año que tuvo menos retraso.
group <- group_by(flights, year, month, day)
group2<- summarize(group, retraso=mean(dep_delay, na.rm=TRUE))
## `summarise()` has grouped output by 'year', 'month'. You can override using the
## `.groups` argument.
arrange(group2, retraso)
## # A tibble: 365 × 4
## # Groups:   year, month [12]
##     year month   day retraso
##    <int> <int> <int>   <dbl>
##  1  2013     9    24 -1.33  
##  2  2013    10     2 -0.920 
##  3  2013    11     9 -0.629 
##  4  2013     9     6 -0.398 
##  5  2013     9     5 -0.388 
##  6  2013    10    29 -0.349 
##  7  2013     9     7 -0.250 
##  8  2013    11     5 -0.183 
##  9  2013    10    19 -0.107 
## 10  2013    10     1 -0.0990
## # … with 355 more rows
#La fecha con menor retraso fue el 24 de Septiembre del 2013.

#17 Obtén el número de vuelos por aerlorínea y destino
#Identifica aerolínea y destino con mayor número de vuelos ordenando el resultado anterior de forma descendente
conteo <- flights %>%
  group_by(carrier, dest) %>%
  summarize(total = n())
## `summarise()` has grouped output by 'carrier'. You can override using the
## `.groups` argument.
conteo
## # A tibble: 314 × 3
## # Groups:   carrier [16]
##    carrier dest  total
##    <chr>   <chr> <int>
##  1 9E      ATL      59
##  2 9E      AUS       2
##  3 9E      AVL      10
##  4 9E      BGR       1
##  5 9E      BNA     474
##  6 9E      BOS     914
##  7 9E      BTV       2
##  8 9E      BUF     833
##  9 9E      BWI     856
## 10 9E      CAE       3
## # … with 304 more rows
#La aerolínea con mayor número de vuelos es 'DL' (Delta) y el destino con el mayor número de vuelos es ATL (Atlanta).

#18 Obtén la máxima distancias recorrida por aerolínea y destino, ordena en forma descendente por máxima distancia recorrida
dist <- flights %>%
  group_by(carrier,dest) %>%
  summarize(max_distancia = sum(distance))
## `summarise()` has grouped output by 'carrier'. You can override using the
## `.groups` argument.
dist
## # A tibble: 314 × 3
## # Groups:   carrier [16]
##    carrier dest  max_distancia
##    <chr>   <chr>         <dbl>
##  1 9E      ATL           44784
##  2 9E      AUS            3042
##  3 9E      AVL            5990
##  4 9E      BGR             378
##  5 9E      BNA          362500
##  6 9E      BOS          170918
##  7 9E      BTV             516
##  8 9E      BUF          250247
##  9 9E      BWI          157504
## 10 9E      CAE            1851
## # … with 304 more rows
#La aerolínea con mayor distancia recorrida es 'UA' (United Airlines) y el destino es SFO (San Fransisco).

#19 Obtén el tiempo de vuelo por aerolínea y destino, ordena en forma descendente por la suma_tiempo_vuelo e identifica las 3 aerolíneas con sus destinos que han volado más tiempo
tiempo <- flights %>%
  group_by(carrier, dest) %>%
  summarize(suma_tiempo_vuelo = sum(air_time, na.rm = TRUE))
## `summarise()` has grouped output by 'carrier'. You can override using the
## `.groups` argument.
tiempo
## # A tibble: 314 × 3
## # Groups:   carrier [16]
##    carrier dest  suma_tiempo_vuelo
##    <chr>   <chr>             <dbl>
##  1 9E      ATL                6726
##  2 9E      AUS                 444
##  3 9E      AVL                 922
##  4 9E      BGR                   0
##  5 9E      BNA               52335
##  6 9E      BOS               33201
##  7 9E      BTV                  97
##  8 9E      BUF               44750
##  9 9E      BWI               31620
## 10 9E      CAE                 313
## # … with 304 more rows
#Las aerolineás con más tiempo volado son United Airlines (SFO, LAX, IAH), American Airlines (DFW, LAX, MIA) y Delta (ATL, LAX, SFO).

Renombrando campos en un data frame con rename()

#20 En la consulta 18 renombra el campo max_distancia por distancia_máxima_recorrida
nombre <- dist %>% rename(distancia_máxima_recorrida = max_distancia)
nombre
## # A tibble: 314 × 3
## # Groups:   carrier [16]
##    carrier dest  distancia_máxima_recorrida
##    <chr>   <chr>                      <dbl>
##  1 9E      ATL                        44784
##  2 9E      AUS                         3042
##  3 9E      AVL                         5990
##  4 9E      BGR                          378
##  5 9E      BNA                       362500
##  6 9E      BOS                       170918
##  7 9E      BTV                          516
##  8 9E      BUF                       250247
##  9 9E      BWI                       157504
## 10 9E      CAE                         1851
## # … with 304 more rows

```