Autores
-Johann Tul
-Bryan Venegas
Seleccionamos las 3 primeras columnas del dataset mtcars y mostramos la cabecera
installed.packages("dplyr")
## Package LibPath Version Priority Depends Imports LinkingTo Suggests
## Enhances License License_is_FOSS License_restricts_use OS_type Archs
## MD5sum NeedsCompilation Built
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
##
## 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
(mtcars)
## 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
## 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 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
## 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
## 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
## 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
## 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
## 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
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
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
select(mtcars)
## data frame with 0 columns and 32 rows
Utiliza la ayuda ?select_helpers para que observes el resultado
?select_helpers
## starting httpd help server ... done
Sirve oara saber la función de select
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
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
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 selecciona las columnas drat, am
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
Selecciona las columnas con el caracter “a”
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
SELECCIONA LAS FILAS QUE SEAN MAYOR QUE 20 Y SEAN MENOR IGUAL A 4
Seleccionar los sujetos con tipo de transmisión (am) 1 que, además, tienen 6 cilindros o menos
head(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
Seleccionar los sujetos que bien consumen menos de 21 mpg o bien tienen menos de 3 carburantes (carb) y menos de 4 engranajes (gear)
head(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 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
Ordena por cilindrada (cyl) y por desplazamiento (disp)
head(select(mtcars, cyl, disp, everything()))
## cyl disp mpg hp drat wt qsec vs am gear carb
## Mazda RX4 6 160 21.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 6 160 21.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 4 108 22.8 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 6 258 21.4 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 8 360 18.7 175 3.15 3.440 17.02 0 0 3 2
## Valiant 6 225 18.1 105 2.76 3.460 20.22 1 0 3 1
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.
mtcars$kg <- (mtcars$wt*0.45)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb kg
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1.17900
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 1.29375
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 1.04400
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 1.44675
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 1.54800
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 1.55700
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:
agrupacion <- group_by(mtcars, disp)
agrupacion
## # A tibble: 32 x 12
## # Groups: disp [27]
## mpg cyl disp hp drat wt qsec vs am gear carb kg
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 1.18
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 1.29
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 1.04
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 1.45
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 1.55
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 1.56
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 1.61
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 1.44
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 1.42
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 1.55
## # ... with 22 more rows
summarise(agrupacion, mean(disp))
## # A tibble: 27 x 2
## disp `mean(disp)`
## <dbl> <dbl>
## 1 71.1 71.1
## 2 75.7 75.7
## 3 78.7 78.7
## 4 79 79
## 5 95.1 95.1
## 6 108 108
## 7 120. 120.
## 8 120. 120.
## 9 121 121
## 10 141. 141.
## # ... with 17 more rows
summarise(group_by(mtcars, cyl), max = max(disp)) (Explica que resultado obtienes ?)
summarise(group_by(mtcars, cyl), max = max(disp))
## # A tibble: 3 x 2
## cyl max
## <dbl> <dbl>
## 1 4 147.
## 2 6 258
## 3 8 472
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
SELECCIONA DIFERENTES VARIABLES EN ESTE CASO MPG HASTA DISP
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 LAS VARIABLES QUE CONTENGAN EL CARACTER “a”
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
mtcars_filtered = filter(mtcars, wt > 1.5)
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 x 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
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 x 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
Es obligatorio utilizar las funciones del paquete dplyr
y recomendable utilizar pipes %>% para los siguientes
ejercicios
library(dplyr)
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
Descarga el archivo y Guarda los datos en una variable llamada “vuelos”
#vuelos <- read.csv("C:/Users/jorda/Downloads/vuelos.csv", header = T, sep = ",", dec = ".")
#vuelos
Selecciona los vuelos con destino SFO u OAK
utilizando las funciones del paquete dplyr. ¿Con cuantos vuelos nos
quedamos?
vuelos_destino1 <- filter(vuelos, dest=="SFO")
head(vuelos_destino1)
## 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
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_retrasos <- select(vuelos, arr_delay)
head(vuelos_retrasos)
## arr_delay
## 1 -10
## 2 -9
## 3 -8
## 4 3
## 5 -3
## 6 -7
Encuentra 4 maneras diferentes de utilizar la función
select para seleccionar las variables relacionadas con los
retrasos (delay)
head(select(vuelos, dep_delay))
## dep_delay
## 1 0
## 2 1
## 3 -8
## 4 3
## 5 5
## 6 -1
Agrupa los vuelos por fecha y calcula: media, mediana y cuartil 75 de los retrasos en los vuelos por hora
head(select(vuelos, dep_delay))
## dep_delay
## 1 0
## 2 1
## 3 -8
## 4 3
## 5 5
## 6 -1
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
mean(vuelos$dep_delay, na.rm = T)
## [1] 9.359078
mean(vuelos$arr_delay, na.rm = T)
## [1] 7.592729
head(filter(vuelos, dep_delay>10,arr_delay>10))
## date hour minute dep arr dep_delay arr_delay carrier flight dest
## 9 2011-01-09 14 43 1443 1554 43 44 AA 428 DFW
## 10 2011-01-10 14 43 1443 1553 43 43 AA 428 DFW
## 11 2011-01-11 14 29 1429 1539 29 29 AA 428 DFW
## 17 2011-01-17 15 30 1530 1634 90 84 AA 428 DFW
## 20 2011-01-20 15 7 1507 1622 67 72 AA 428 DFW
## 31 2011-01-31 14 41 1441 1553 41 43 AA 428 DFW
## plane cancelled time dist
## 9 N476AA 0 41 224
## 10 N504AA 0 45 224
## 11 N565AA 0 42 224
## 17 N518AA 0 48 224
## 20 N425AA 0 42 224
## 31 N505AA 0 39 224