Cargar la librería nycflights13 y tidyverse
Del conjunto de datos flights encuentra
library(nycflights13)
library(dplyr)
glimpse(flights)
## Observations: 336,776
## Variables: 19
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013...
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ dep_time <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 558, 55...
## $ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 600, 60...
## $ dep_delay <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2, -2,...
## $ arr_time <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 753, 8...
## $ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 745, 8...
## $ arr_delay <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -3, 7,...
## $ carrier <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV", "B6"...
## $ flight <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79, 301...
## $ tailnum <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN", "N...
## $ origin <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR", "LG...
## $ dest <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL", "IA...
## $ air_time <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138, 149...
## $ distance <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 944, 73...
## $ hour <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6...
## $ minute <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59...
## $ time_hour <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013-01-0...
data("flights")
flights<- flights
vuelos_retraso<- filter(flights,arr_delay >= 120)
head(vuelos_retraso,10)
## # A tibble: 10 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 811 630 101 1047 830
## 2 2013 1 1 848 1835 853 1001 1950
## 3 2013 1 1 957 733 144 1056 853
## 4 2013 1 1 1114 900 134 1447 1222
## 5 2013 1 1 1505 1310 115 1638 1431
## 6 2013 1 1 1525 1340 105 1831 1626
## 7 2013 1 1 1549 1445 64 1912 1656
## 8 2013 1 1 1558 1359 119 1718 1515
## 9 2013 1 1 1732 1630 62 2028 1825
## 10 2013 1 1 1803 1620 103 2008 1750
## # ... with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
cantidad_vuelos_retraso<-count(vuelos_retraso)
cantidad_vuelos_retraso
## # A tibble: 1 x 1
## n
## <int>
## 1 10200
Se identifica un total de 10.200 vuelos con retraso en su lugar de destino de todos los vuelos registrados 336.776
destino_miami<- filter(flights,dest == "MIA")
head(destino_miami,10)
## # A tibble: 10 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 542 540 2 923 850
## 2 2013 1 1 606 610 -4 858 910
## 3 2013 1 1 607 607 0 858 915
## 4 2013 1 1 623 610 13 920 915
## 5 2013 1 1 655 700 -5 1002 1020
## 6 2013 1 1 659 700 -1 1008 1015
## 7 2013 1 1 753 755 -2 1056 1110
## 8 2013 1 1 759 800 -1 1057 1127
## 9 2013 1 1 826 715 71 1136 1045
## 10 2013 1 1 856 900 -4 1222 1232
## # ... with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
cantidad_destino_miami<- count(destino_miami)
cantidad_destino_miami
## # A tibble: 1 x 1
## n
## <int>
## 1 11728
Existen un total de 11.728 vuelos con destino a Miami.
destino_miami_delay<- filter(flights,dest == "MIA", arr_delay >=120)
head(destino_miami,10)
## # A tibble: 10 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 542 540 2 923 850
## 2 2013 1 1 606 610 -4 858 910
## 3 2013 1 1 607 607 0 858 915
## 4 2013 1 1 623 610 13 920 915
## 5 2013 1 1 655 700 -5 1002 1020
## 6 2013 1 1 659 700 -1 1008 1015
## 7 2013 1 1 753 755 -2 1056 1110
## 8 2013 1 1 759 800 -1 1057 1127
## 9 2013 1 1 826 715 71 1136 1045
## 10 2013 1 1 856 900 -4 1222 1232
## # ... with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
cantidad_miami_delay<- count(destino_miami_delay)
cantidad_miami_delay
## # A tibble: 1 x 1
## n
## <int>
## 1 218
Existen un total de 2.18 vuelos con destino a Miami con un retraso de 2 horas o más.
resumen_x_destino <- vuelos_retraso %>%
group_by(dest) %>%
summarise(
media_retraso = mean(arr_delay, na.rm = TRUE), # necesario si hay valores perdidos
distancia_media = mean(distance),
numero_vuelos = n(),
)
resumen_x_destino
## # A tibble: 97 x 4
## dest media_retraso distancia_media numero_vuelos
## <chr> <dbl> <dbl> <int>
## 1 ABQ 140. 1826 6
## 2 ACK 156. 199 4
## 3 ALB 180. 143 19
## 4 ATL 196. 757. 582
## 5 AUS 176. 1517. 73
## 6 AVL 173. 583 4
## 7 BDL 168. 116 12
## 8 BGR 170. 378 13
## 9 BHM 167. 866 19
## 10 BNA 184. 758. 259
## # ... with 87 more rows
nuevo_frame<- data.frame(resumen_x_destino)
library(ggplot2)
nuevo_frame %>%
ggplot(aes(x = distancia_media , y = media_retraso)) +
geom_point(aes(color = dest, size = desc(numero_vuelos)), alpha = 0.5) +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Utilizando los datasets vistos en clase, flights, weather, planes , etc. Responde a las siguientes cuestiones:
library(nycflights13)
data("planes")
planes <- planes
flights2 <- flights %>%
group_by(tailnum) %>%
summarise(numero_vuelos = n() ) %>%
filter(numero_vuelos>=100)
head(flights2,10)
## # A tibble: 10 x 2
## tailnum numero_vuelos
## <chr> <int>
## 1 N0EGMQ 371
## 2 N10156 153
## 3 N10575 289
## 4 N11106 129
## 5 N11107 148
## 6 N11109 148
## 7 N11113 138
## 8 N11119 148
## 9 N11121 154
## 10 N11127 124
df<-merge(x=flights2,y=planes,by="tailnum",all.x=TRUE)
head(df,10)
## tailnum numero_vuelos year type manufacturer model
## 1 N0EGMQ 371 NA <NA> <NA> <NA>
## 2 N10156 153 2004 Fixed wing multi engine EMBRAER EMB-145XR
## 3 N10575 289 2002 Fixed wing multi engine EMBRAER EMB-145LR
## 4 N11106 129 2002 Fixed wing multi engine EMBRAER EMB-145XR
## 5 N11107 148 2002 Fixed wing multi engine EMBRAER EMB-145XR
## 6 N11109 148 2002 Fixed wing multi engine EMBRAER EMB-145XR
## 7 N11113 138 2002 Fixed wing multi engine EMBRAER EMB-145XR
## 8 N11119 148 2002 Fixed wing multi engine EMBRAER EMB-145XR
## 9 N11121 154 2003 Fixed wing multi engine EMBRAER EMB-145XR
## 10 N11127 124 2003 Fixed wing multi engine EMBRAER EMB-145XR
## engines seats speed engine
## 1 NA NA NA <NA>
## 2 2 55 NA Turbo-fan
## 3 2 55 NA Turbo-fan
## 4 2 55 NA Turbo-fan
## 5 2 55 NA Turbo-fan
## 6 2 55 NA Turbo-fan
## 7 2 55 NA Turbo-fan
## 8 2 55 NA Turbo-fan
## 9 2 55 NA Turbo-fan
## 10 2 55 NA Turbo-fan
Ojo : antes de hacer arrange es importante hacer ungroup() (repasar apuntes dónde viene)
flights3 <- flights %>%
group_by(origin,year,month,day,hour) %>%
summarise(retraso_medio = mean(dep_delay))%>%
ungroup(30)%>%
arrange(desc(retraso_medio))
head(flights3,30)
## # A tibble: 30 x 6
## origin year month day hour retraso_medio
## <chr> <int> <int> <int> <dbl> <dbl>
## 1 LGA 2013 3 18 22 155
## 2 LGA 2013 6 13 22 149
## 3 JFK 2013 7 10 22 148.
## 4 JFK 2013 3 18 21 143.
## 5 JFK 2013 8 8 22 143.
## 6 JFK 2013 9 12 21 142.
## 7 LGA 2013 3 11 5 142
## 8 LGA 2013 6 16 22 140
## 9 LGA 2013 5 19 22 139
## 10 LGA 2013 5 20 22 135
## # ... with 20 more rows
3.Una vez tenga el dataset resultante, cruzar con weather utilizando semi_join, para quedarte solo con los registros en weather que cruzan con el dataset de flights filtrado. Una vez tengas el resultado grafica en la x la variable precip , en la y wind_speed y como color de los puntos usa temp.
data("weather")
weather<- weather
dw<-semi_join(x=weather,y=flights3,by="origin",all.x=TRUE)
head(dw,10)
## # A tibble: 10 x 15
## origin year month day hour temp dewp humid wind_dir wind_speed
## <chr> <int> <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 EWR 2013 1 1 1 39.0 26.1 59.4 270 10.4
## 2 EWR 2013 1 1 2 39.0 27.0 61.6 250 8.06
## 3 EWR 2013 1 1 3 39.0 28.0 64.4 240 11.5
## 4 EWR 2013 1 1 4 39.9 28.0 62.2 250 12.7
## 5 EWR 2013 1 1 5 39.0 28.0 64.4 260 12.7
## 6 EWR 2013 1 1 6 37.9 28.0 67.2 240 11.5
## 7 EWR 2013 1 1 7 39.0 28.0 64.4 240 15.0
## 8 EWR 2013 1 1 8 39.9 28.0 62.2 250 10.4
## 9 EWR 2013 1 1 9 39.9 28.0 62.2 260 15.0
## 10 EWR 2013 1 1 10 41 28.0 59.6 260 13.8
## # ... with 5 more variables: wind_gust <dbl>, precip <dbl>, pressure <dbl>,
## # visib <dbl>, time_hour <dttm>
library(ggplot2)
dw %>%
ggplot(aes(x = precip , y = wind_speed)) +
geom_point(aes(color = temp, alpha = 0.5))
## Warning: Removed 4 rows containing missing values (geom_point).