Utilizando la base de datos interna
mtcars, resolver los siguientes enunciados::
1
Seleccionamos las 3 primeras columnas del dataset mtcars y mostramos la cabecera
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
head(select(mtcars, 1:3))
## mpg cyl disp
## Mazda RX4 21.0 6 160
## Mazda RX4 Wag 21.0 6 160
## Datsun 710 22.8 4 108
## Hornet 4 Drive 21.4 6 258
## Hornet Sportabout 18.7 8 360
## Valiant 18.1 6 225
2
Utiliza la ayuda ?select_helpers para que observes el resultado
#?select_helpers
#Permite observar la funcion select con mas detalles en Help
3
Seleccionar las columnas que empiezan por d
head(select(mtcars, starts_with("d")))
## disp drat
## Mazda RX4 160 3.90
## Mazda RX4 Wag 160 3.90
## Datsun 710 108 3.85
## Hornet 4 Drive 258 3.08
## Hornet Sportabout 360 3.15
## Valiant 225 2.76
4
Seleccionar las columnas que terminan por p
head(select(mtcars, ends_with("p")))
## disp hp
## Mazda RX4 160 110
## Mazda RX4 Wag 160 110
## Datsun 710 108 93
## Hornet 4 Drive 258 110
## Hornet Sportabout 360 175
## Valiant 225 105
5
head(select( mtcars, -drat, -am )) (Explica que resultado obtienes ?)
head(select(mtcars, -drat, -am))
## mpg cyl disp hp wt qsec vs gear carb
## Mazda RX4 21.0 6 160 110 2.620 16.46 0 4 4
## Mazda RX4 Wag 21.0 6 160 110 2.875 17.02 0 4 4
## Datsun 710 22.8 4 108 93 2.320 18.61 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.215 19.44 1 3 1
## Hornet Sportabout 18.7 8 360 175 3.440 17.02 0 3 2
## Valiant 18.1 6 225 105 3.460 20.22 1 3 1
#NO SE SELECCIONAN LAS COLUMNAS -drat Y -am por que las descartan con el (-)
6
head(select( mtcars, contains( "a" ) )) (Explica que resultado obtienes ?)
head(select( mtcars, contains( "a" ) ))
## drat am gear carb
## Mazda RX4 3.90 1 4 4
## Mazda RX4 Wag 3.90 1 4 4
## Datsun 710 3.85 1 4 1
## Hornet 4 Drive 3.08 0 3 1
## Hornet Sportabout 3.15 0 3 2
## Valiant 2.76 0 3 1
#contains selecciona las columnas que contengan el caracter "a"
7
head(filter( mtcars, mpg > 20, gear == 4)) (Explica que resultado obtienes ?)
head(filter( mtcars, mpg > 20, gear == 4))
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#filter() nos permite seleccionar las filas con la condicion de mpg sea mayor a 20 y que gear sea igual a 4
8
Seleccionar los sujetos con tipo de transmisión (am) 1 que, además, tienen 6 cilindros o menos
filter(mtcars, am == 1 & cyl<=6)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
9
Seleccionar los sujetos que bien consumen menos de 21 mpg o bien tienen menos de 3 carburantes (carb) y menos de 4 engranajes (gear)
filter(mtcars, mpg < 21 | carb < 3 & gear < 4)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
10
Ordena por cilindrada (cyl) y por desplazamiento (disp)
select(mtcars, cyl, disp, everything())
## cyl disp mpg hp drat wt qsec vs am gear carb
## Mazda RX4 6 160.0 21.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 6 160.0 21.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 4 108.0 22.8 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 6 258.0 21.4 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 8 360.0 18.7 175 3.15 3.440 17.02 0 0 3 2
## Valiant 6 225.0 18.1 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 8 360.0 14.3 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 4 146.7 24.4 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 4 140.8 22.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 6 167.6 19.2 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 6 167.6 17.8 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 8 275.8 16.4 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 8 275.8 17.3 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 8 275.8 15.2 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 8 472.0 10.4 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 8 460.0 10.4 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 8 440.0 14.7 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 4 78.7 32.4 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 4 75.7 30.4 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 4 71.1 33.9 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 4 120.1 21.5 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 8 318.0 15.5 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 8 304.0 15.2 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 8 350.0 13.3 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 8 400.0 19.2 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 4 79.0 27.3 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 4 120.3 26.0 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 4 95.1 30.4 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 8 351.0 15.8 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 6 145.0 19.7 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 8 301.0 15.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 4 121.0 21.4 109 4.11 2.780 18.60 1 1 4 2
11
Crea una nueva columna que indique los kilogramos que pesa el coche, sabiendo que 1 libra = 0.45 kg. La variable wt indica el peso en libras.
wt<-mtcars[,7]
wt
## [1] 16.46 17.02 18.61 19.44 17.02 20.22 15.84 20.00 22.90 18.30 18.90 17.40
## [13] 17.60 18.00 17.98 17.82 17.42 19.47 18.52 19.90 20.01 16.87 17.30 15.41
## [25] 17.05 18.90 16.70 16.90 14.50 15.50 14.60 18.60
wt_Kg<-wt*0.45
wt_Kg
## [1] 7.4070 7.6590 8.3745 8.7480 7.6590 9.0990 7.1280 9.0000 10.3050
## [10] 8.2350 8.5050 7.8300 7.9200 8.1000 8.0910 8.0190 7.8390 8.7615
## [19] 8.3340 8.9550 9.0045 7.5915 7.7850 6.9345 7.6725 8.5050 7.5150
## [28] 7.6050 6.5250 6.9750 6.5700 8.3700
mtcars_mod<-cbind(mtcars,wt_Kg)
mtcars_mod
## mpg cyl disp hp drat wt qsec vs am gear carb wt_Kg
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 7.4070
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 7.6590
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8.3745
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 8.7480
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 7.6590
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 9.0990
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 7.1280
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 9.0000
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 10.3050
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 8.2350
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 8.5050
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 7.8300
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 7.9200
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 8.1000
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 8.0910
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 8.0190
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 7.8390
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8.7615
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8.3340
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8.9550
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 9.0045
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 7.5915
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 7.7850
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 6.9345
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 7.6725
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8.5050
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 7.5150
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 7.6050
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 6.5250
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 6.9750
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 6.5700
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8.3700
12
La función summarise() agrupa los valores en una tabla de acuerdo a la función que indiquemos. Calcula la media de disp usando la función summarise:
mtcars_mod1<-summarise(mtcars_mod, mean(disp))
mtcars_mod1
## mean(disp)
## 1 230.7219
13
summarise(group_by(mtcars, cyl), max = max(disp)) (Explica que resultado obtienes ?)
summarise(group_by(mtcars, cyl), max = max(disp))
## # A tibble: 3 × 2
## cyl max
## <dbl> <dbl>
## 1 4 147.
## 2 6 258
## 3 8 472
#Agrupamos las variables cyl por el m?ximo de la variable disp
14
mtcars %>% select( mpg:disp )%>% head (Explica que resultado obtienes ?)
mtcars %>% select(mpg:disp)%>% head
## mpg cyl disp
## Mazda RX4 21.0 6 160
## Mazda RX4 Wag 21.0 6 160
## Datsun 710 22.8 4 108
## Hornet 4 Drive 21.4 6 258
## Hornet Sportabout 18.7 8 360
## Valiant 18.1 6 225
#Comando pipe seleciona diferentes variables como en este caso va desde mpg hasta disp
15
head(select(select(mtcars, contains("a")), -drat, -am)) (Explica que resultado obtienes ?)
head(select(select(mtcars, contains("a")), -drat, -am))
## gear carb
## Mazda RX4 4 4
## Mazda RX4 Wag 4 4
## Datsun 710 4 1
## Hornet 4 Drive 3 1
## Hornet Sportabout 3 2
## Valiant 3 1
#Selecciona todas las variables que contengan el caracter "a" menos las columnas drat y am
16
Utilizando pipes ejecuta el ejercicio 15
mtcars %>% select(contains("a"), -drat, -am) %>% head()
## gear carb
## Mazda RX4 4 4
## Mazda RX4 Wag 4 4
## Datsun 710 4 1
## Hornet 4 Drive 3 1
## Hornet Sportabout 3 2
## Valiant 3 1
17
mtcars_filtered = filter(mtcars, wt > 1.5)
mtcars_grouped = group_by(mtcars_filtered, cyl)
summarise(mtcars_grouped, mn = mean(mpg), sd = sd(mpg)) (Explica que resultado obtienes ?)
mtcars_filtered = filter(mtcars, wt > 1.5)
mtcars_grouped = group_by(mtcars_filtered, cyl)
summarise(mtcars_grouped, mn = mean(mpg), sd = sd(mpg))
## # A tibble: 3 × 3
## cyl mn sd
## <dbl> <dbl> <dbl>
## 1 4 26.7 4.51
## 2 6 19.7 1.45
## 3 8 15.1 2.56
#Se esta agrupando por la variable cyl y calcula la media y desviacion estandar a los datos que sean mayores a 1.5
18
Utilizando pipes ejecuta el ejercicio 17
mtcars %>% filter(wt > 1.5)%>%
group_by(cyl) %>%
summarise(mn = mean(mpg), sd = sd(mpg)) %>% head()
## # A tibble: 3 × 3
## cyl mn sd
## <dbl> <dbl> <dbl>
## 1 4 26.7 4.51
## 2 6 19.7 1.45
## 3 8 15.1 2.56
19
Es obligatorio utilizar las funciones del paquete `dplyr` y recomendable utilizar pipes `%>%` para los siguientes ejercicios
#library(dplyr)
20
Descarga el fichero de datos `vuelos.csv` situado en <http://gauss.inf.um.es/datos/>; en local o localiza la url donde se encuentra
vuelos<-read.table(file = "https://gauss.inf.um.es/datos/vuelos.csv", header = T, sep = ",")
head(vuelos)
## date hour minute dep arr dep_delay arr_delay carrier flight dest
## 1 2011-01-01 14 0 1400 1500 0 -10 AA 428 DFW
## 2 2011-01-02 14 1 1401 1501 1 -9 AA 428 DFW
## 3 2011-01-03 13 52 1352 1502 -8 -8 AA 428 DFW
## 4 2011-01-04 14 3 1403 1513 3 3 AA 428 DFW
## 5 2011-01-05 14 5 1405 1507 5 -3 AA 428 DFW
## 6 2011-01-06 13 59 1359 1503 -1 -7 AA 428 DFW
## plane cancelled time dist
## 1 N576AA 0 40 224
## 2 N557AA 0 45 224
## 3 N541AA 0 48 224
## 4 N403AA 0 39 224
## 5 N492AA 0 44 224
## 6 N262AA 0 45 224
21
Descarga el archivo y Guarda los datos en una variable llamada `vuelos`
#web1 <- "https://gauss.inf.um.es/datos/vuelos.csv"
#web1
#download.file(web1, "vuelos.csv") permite descargar el archivo y con el tipo que desea descargar
#vuelos <- read.table(file = "vuelos.csv", header = T, sep = ",")
#View(vuelos)
* Download.file(web1, "vuelos.csv") permite descargar el archivo y con el tipo que desea descargar.
* Volvemos a leer el archivo descargado con el nombre y tipo que se descargo
22
Selecciona los vuelos con destino `SFO` u `OAK` utilizando las funciones del paquete dplyr. ¿Con cuantos vuelos nos quedamos?
destino <- filter(vuelos, dest=="SFO" | dest=="OAK")
head(destino)
## date hour minute dep arr dep_delay arr_delay carrier flight dest
## 373 2011-01-31 8 51 851 1052 1 -27 CO 170 SFO
## 389 2011-01-31 11 29 1129 1351 4 1 CO 270 SFO
## 402 2011-01-31 14 32 1432 1656 7 5 CO 370 SFO
## 436 2011-01-31 17 48 1748 2001 3 -4 CO 570 SFO
## 467 2011-01-31 21 43 2143 2338 50 24 CO 770 SFO
## 468 2011-01-31 7 29 729 1002 -1 2 CO 771 SFO
## plane cancelled time dist
## 373 N35407 0 225 1635
## 389 N37420 0 228 1635
## 402 N27213 0 229 1635
## 436 N75436 0 236 1635
## 467 N37281 0 224 1635
## 468 N26226 0 237 1635
nrow(destino)
## [1] 1121
23
Selecciona los vuelos que se han retrasado más de una hora. ¿Cuál es el destino que más se retrasa en proporción al número de vuelos?
vuelos %>% select(arr_delay, dest)%>%
filter(arr_delay>60) %>% head()
## arr_delay dest
## 17 84 DFW
## 20 72 DFW
## 74 69 DFW
## 97 126 DFW
## 99 70 DFW
## 129 80 MIA
24
Encuentra 4 maneras diferentes de utilizar la función `select` para seleccionar las variables relacionadas con los retrasos (delay)
v_delay <- c("dep_delay", "arr_delay")
head(select(vuelos, v_delay, everything()))
## Note: Using an external vector in selections is ambiguous.
## ℹ Use `all_of(v_delay)` instead of `v_delay` to silence this message.
## ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This message is displayed once per session.
## dep_delay arr_delay date hour minute dep arr carrier flight dest
## 1 0 -10 2011-01-01 14 0 1400 1500 AA 428 DFW
## 2 1 -9 2011-01-02 14 1 1401 1501 AA 428 DFW
## 3 -8 -8 2011-01-03 13 52 1352 1502 AA 428 DFW
## 4 3 3 2011-01-04 14 3 1403 1513 AA 428 DFW
## 5 5 -3 2011-01-05 14 5 1405 1507 AA 428 DFW
## 6 -1 -7 2011-01-06 13 59 1359 1503 AA 428 DFW
## plane cancelled time dist
## 1 N576AA 0 40 224
## 2 N557AA 0 45 224
## 3 N541AA 0 48 224
## 4 N403AA 0 39 224
## 5 N492AA 0 44 224
## 6 N262AA 0 45 224
head(select(vuelos, contains("de")))
## dep dep_delay arr_delay dest
## 1 1400 0 -10 DFW
## 2 1401 1 -9 DFW
## 3 1352 -8 -8 DFW
## 4 1403 3 3 DFW
## 5 1405 5 -3 DFW
## 6 1359 -1 -7 DFW
head(select(vuelos, matches(".t.")))
## date minute
## 1 2011-01-01 0
## 2 2011-01-02 1
## 3 2011-01-03 52
## 4 2011-01-04 3
## 5 2011-01-05 5
## 6 2011-01-06 59
vuelos %>% select(-contains("d")) %>% head()
## hour minute arr carrier flight plane time
## 1 14 0 1500 AA 428 N576AA 40
## 2 14 1 1501 AA 428 N557AA 45
## 3 13 52 1502 AA 428 N541AA 48
## 4 14 3 1513 AA 428 N403AA 39
## 5 14 5 1507 AA 428 N492AA 44
## 6 13 59 1503 AA 428 N262AA 45
25
Agrupa los vuelos por fecha y calcula: media, mediana y cuartil 75 de los retrasos en los vuelos por hora
vuelos %>% filter(arr_delay > 60)%>%
group_by(date) %>%
summarise(mn = mean(arr_delay), md = median(arr_delay), qu75=quantile(arr_delay)) %>% head
## `summarise()` has grouped output by 'date'. You can override using the
## `.groups` argument.
## # A tibble: 6 × 4
## # Groups: date [2]
## date mn md qu75
## <chr> <dbl> <dbl> <dbl>
## 1 2011-01-01 83.5 71 61
## 2 2011-01-01 83.5 71 64
## 3 2011-01-01 83.5 71 71
## 4 2011-01-01 83.5 71 107
## 5 2011-01-01 83.5 71 137
## 6 2011-01-02 114. 101 62
26
Utilizando pipes calcula la media de retraso en los vuelos por día y hora, la cantidad de vuelos por día y hora y luego muestra solo los casos para los cuales haya más de 10
vuelos %>% filter(arr_delay > 10)%>%
group_by(date, hour) %>%
summarise(mn = mean(arr_delay), cantidad =length(arr_delay)) %>% head
## `summarise()` has grouped output by 'date'. You can override using the
## `.groups` argument.
## # A tibble: 6 × 4
## # Groups: date [1]
## date hour mn cantidad
## <chr> <int> <dbl> <int>
## 1 2011-01-01 0 24 1
## 2 2011-01-01 7 37.6 8
## 3 2011-01-01 8 32 2
## 4 2011-01-01 9 22.5 13
## 5 2011-01-01 10 22.4 20
## 6 2011-01-01 11 31.8 18