En el sitio de datos abiertos se encuentra una base de datos de los accidentes en motos del año 2015-2019 en Medellín.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.4
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
acc_moto <- read.csv("Accidentalidad_con_motos_municipio_de_Medell_n_a_o_2019.csv")
summary(acc_moto)
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE
## Min. :1470349 Min. :2015 08/04/2017 12:00:00 AM: 140 1899-12-31T17:00:00.000: 1800
## 1st Qu.:1522052 1st Qu.:2016 08/18/2015 12:00:00 AM: 130 1899-12-31T18:00:00.000: 1762
## Median :1571616 Median :2017 02/11/2016 12:00:00 AM: 128 1899-12-31T07:00:00.000: 1671
## Mean :1572456 Mean :2017 07/27/2016 12:00:00 AM: 120 1899-12-31T16:00:00.000: 1634
## 3rd Qu.:1622950 3rd Qu.:2018 08/28/2015 12:00:00 AM: 118 1899-12-31T17:30:00.000: 1630
## Max. :1676894 Max. :2019 06/07/2017 12:00:00 AM: 116 1899-12-31T15:00:00.000: 1591
## (Other) :118396 (Other) :109060
## CLASE_ACCIDENTE GRAVEDAD_ACCIDENTE DIRECCION ZONA Diseño.Vía
## : 3 HERIDO :94610 Carrera 999 Con Calle 999: 1271 COMUNA 10:18128 Tramo de via :82303
## Atropello :11243 MUERTO : 431 Calle 999 Con Carrera 999: 655 COMUNA 5 :12374 Interseccion :19136
## Caida Ocupante:15805 SOLO DAÑOS:24107 Carrera 80 Con Calle 50 : 319 COMUNA 11:11072 Lote o Predio:13671
## Choque :64517 Carrera 80 Con Calle 65 : 311 COMUNA 14:10792 Glorieta : 2059
## Incendio : 6 Carrera 80 Con Calle 80 : 278 COMUNA 7 :10726 Paso Elevado : 651
## Otro :20920 Carrera 65 Con Calle 80 : 262 COMUNA 16: 9787 Puente : 469
## Volcamiento : 6654 (Other) :116052 (Other) :46269 (Other) : 859
acc_moto <- acc_moto %>%
mutate(RADICADO = as.factor(RADICADO),
AÑO_ACCIDENTE = as.factor(AÑO_ACCIDENTE))
summary(acc_moto)
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE CLASE_ACCIDENTE
## 1638618: 2 2015:27238 08/04/2017 12:00:00 AM: 140 1899-12-31T17:00:00.000: 1800 : 3
## 1640023: 2 2016:28595 08/18/2015 12:00:00 AM: 130 1899-12-31T18:00:00.000: 1762 Atropello :11243
## 1641667: 2 2017:27050 02/11/2016 12:00:00 AM: 128 1899-12-31T07:00:00.000: 1671 Caida Ocupante:15805
## 1647286: 2 2018:24656 07/27/2016 12:00:00 AM: 120 1899-12-31T16:00:00.000: 1634 Choque :64517
## 1647440: 2 2019:11609 08/28/2015 12:00:00 AM: 118 1899-12-31T17:30:00.000: 1630 Incendio : 6
## 1470349: 1 06/07/2017 12:00:00 AM: 116 1899-12-31T15:00:00.000: 1591 Otro :20920
## (Other):119137 (Other) :118396 (Other) :109060 Volcamiento : 6654
## GRAVEDAD_ACCIDENTE DIRECCION ZONA Diseño.Vía
## HERIDO :94610 Carrera 999 Con Calle 999: 1271 COMUNA 10:18128 Tramo de via :82303
## MUERTO : 431 Calle 999 Con Carrera 999: 655 COMUNA 5 :12374 Interseccion :19136
## SOLO DAÑOS:24107 Carrera 80 Con Calle 50 : 319 COMUNA 11:11072 Lote o Predio:13671
## Carrera 80 Con Calle 65 : 311 COMUNA 14:10792 Glorieta : 2059
## Carrera 80 Con Calle 80 : 278 COMUNA 7 :10726 Paso Elevado : 651
## Carrera 65 Con Calle 80 : 262 COMUNA 16: 9787 Puente : 469
## (Other) :116052 (Other) :46269 (Other) : 859
radicados_duplicados <- acc_moto$RADICADO[duplicated(acc_moto$RADICADO)]
radicados_duplicados
## [1] 1641667 1638618 1640023 1647440 1647286
## 119143 Levels: 1470349 1470356 1470357 1470426 1470427 1470428 1470431 1470432 1470438 1470445 1470449 1470450 ... 1676894
registros_rad_dup <- acc_moto %>%
filter(RADICADO %in% radicados_duplicados) %>%
arrange(RADICADO)
registros_rad_dup
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE CLASE_ACCIDENTE GRAVEDAD_ACCIDENTE
## 1 1638618 2018 08/14/2018 12:00:00 AM 1899-12-31T17:35:00.000 Choque SOLO DAÑOS
## 2 1638618 2018 10/14/2018 12:00:00 AM 1899-12-31T17:35:00.000 Choque SOLO DAÑOS
## 3 1640023 2018 08/27/2018 12:00:00 AM 1899-12-31T19:30:00.000 Choque HERIDO
## 4 1640023 2018 10/27/2018 12:00:00 AM 1899-12-31T19:30:00.000 Choque HERIDO
## 5 1641667 2018 09/11/2018 12:00:00 AM 1899-12-31T17:55:00.000 Choque HERIDO
## 6 1641667 2018 10/11/2018 12:00:00 AM 1899-12-31T17:55:00.000 Choque HERIDO
## 7 1647286 2018 10/26/2018 12:00:00 AM 1899-12-31T12:50:00.000 Otro HERIDO
## 8 1647286 2018 12/26/2018 12:00:00 AM 1899-12-31T12:10:00.000 Otro HERIDO
## 9 1647440 2018 10/26/2018 12:00:00 AM 1899-12-31T09:00:00.000 Choque HERIDO
## 10 1647440 2018 12/26/2018 12:00:00 AM 1899-12-31T09:10:00.000 Choque HERIDO
## DIRECCION ZONA Diseño.Vía
## 1 Carrera 66 Con Calle 34 A COMUNA 11 Tramo de via
## 2 Carrera 66 Con Calle 34 A COMUNA 11 Tramo de via
## 3 Carrera 80 Con Calle 50 COMUNA 11 Glorieta
## 4 Carrera 80 Con Calle 50 COMUNA 11 Glorieta
## 5 Transversal 76 Con Calle 65 COMUNA 5 Tramo de via
## 6 Transversal 76 Con Calle 65 COMUNA 5 Tramo de via
## 7 Calle 60 Con Carrera 74 COMUNA 7 Tramo de via
## 8 Calle 60 Con Carrera 74 COMUNA 7 Tramo de via
## 9 Calle 30 Con Carrera 80 COMUNA 16 Glorieta
## 10 Calle 30 Con Carrera 80 COMUNA 16 Glorieta
acc_moto1 <- acc_moto %>%
filter(!(RADICADO %in% radicados_duplicados))
unique(substring(acc_moto1$HORA_ACCIDENTE, 1, 4))
## [1] "00:3" "01:0" "02:0" "09:2" "15:0" "23:3" "10:2" "16:3" "14:3" "19:3" "07:1" "16:4" "09:0" "06:5" "07:4" "10:0"
## [17] "14:1" "14:2" "13:0" "22:3" "18:5" "23:0" "21:0" "21:3" "18:3" "07:3" "09:4" "06:3" "04:0" "02:5" "00:1" "00:0"
## [33] "18:4" "13:3" "17:2" "21:2" "07:0" "08:0" "04:3" "01:1" "20:3" "14:0" "11:4" "03:4" "20:0" "07:5" "01:3" "09:3"
## [49] "12:0" "20:2" "15:1" "20:1" "19:4" "21:1" "17:1" "10:3" "16:2" "17:0" "10:1" "15:3" "12:5" "13:4" "11:0" "05:4"
## [65] "04:5" "16:1" "19:2" "18:0" "19:0" "17:3" "17:4" "22:0" "16:5" "15:4" "08:4" "00:2" "12:1" "11:3" "16:0" "12:3"
## [81] "22:5" "08:3" "13:1" "14:5" "08:5" "09:5" "09:1" "21:4" "13:2" "06:0" "17:5" "07:2" "18:1" "14:4" "18:2" "19:5"
## [97] "08:2" "13:5" "06:1" "10:5" "06:4" "22:1" "08:1" "19:1" "20:5" "12:2" "15:2" "05:5" "05:3" "10:4" "00:5" "11:2"
## [113] "06:2" "23:1" "12:4" "22:4" "02:1" "11:1" "23:2" "23:4" "00:4" "02:3" "05:0" "21:5" "20:4" "11:5" "22:2" "05:2"
## [129] "01:5" "15:5" "02:2" "03:0" "04:2" "05:1" "01:4" "03:2" "03:3" "04:1" "02:4" "04:4" "01:2" "03:1" "23:5" "1899"
## [145] "03:5"
casos_hora <- acc_moto1 %>%
filter(substring(acc_moto1$HORA_ACCIDENTE, 1, 4) == "1899")
unique(substring(casos_hora$HORA_ACCIDENTE, 1, 11))
## [1] "1899-12-31T"
acc_moto1 <- acc_moto1 %>%
mutate(HORA_ACCIDENTE = factor(gsub("1899-12-31T", "", HORA_ACCIDENTE)),
fecha = mdy_hms(paste(substring(FECHA_ACCIDENTE, 1, 10),
substring(HORA_ACCIDENTE, 1, 8))))
# Uso de la fecha frecuencia por mes.
table(month(acc_moto1$fecha, label = TRUE))
##
## ene feb mar abr may jun jul ago sep oct nov dic
## 9690 10785 11089 10505 11360 10736 9242 9839 9518 9182 8590 8602
table(wday(acc_moto1$fecha, label = TRUE, abbr = FALSE))
##
## domingo lunes martes miércoles jueves viernes sábado
## 12446 17098 18175 18081 17988 18267 17083
Sin_calle <- acc_moto1 %>%
filter(is.na(str_extract(as.character(DIRECCION), "Calle")))
head(Sin_calle)
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE CLASE_ACCIDENTE GRAVEDAD_ACCIDENTE
## 1 1470450 2015 01/01/2015 12:00:00 AM 07:15:00 Choque HERIDO
## 2 1470458 2015 01/01/2015 12:00:00 AM 10:00:00 Caida Ocupante HERIDO
## 3 1470506 2015 01/02/2015 12:00:00 AM 14:00:00 Otro HERIDO
## 4 1470516 2015 01/02/2015 12:00:00 AM 17:15:00 Otro HERIDO
## 5 1470519 2015 01/02/2015 12:00:00 AM 10:30:00 Otro HERIDO
## 6 1470581 2015 01/03/2015 12:00:00 AM 11:05:00 Choque SOLO DAÑOS
## DIRECCION ZONA Diseño.Vía fecha
## 1 Con Tramo 15 COMUNA 6 Tramo de via 2015-01-01 07:15:00
## 2 Con Tramo 9 COMUNA 8 Tramo de via 2015-01-01 10:00:00
## 3 Con Tramo 17 Jurisdicción Lote o Predio 2015-01-02 14:00:00
## 4 Con Tramo 17 Jurisdicción Lote o Predio 2015-01-02 17:15:00
## 5 Tramo 16 Con Kilometro 5 Jurisdicción Lote o Predio 2015-01-02 10:30:00
## 6 Transversal 39 B Con Carrera 75 COMUNA 11 Interseccion 2015-01-03 11:05:00
Sin_carrera <- acc_moto1 %>%
filter(is.na(str_extract(as.character(DIRECCION), "Carrera")))
head(Sin_carrera)
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE CLASE_ACCIDENTE GRAVEDAD_ACCIDENTE
## 1 1470450 2015 01/01/2015 12:00:00 AM 07:15:00 Choque HERIDO
## 2 1470458 2015 01/01/2015 12:00:00 AM 10:00:00 Caida Ocupante HERIDO
## 3 1470506 2015 01/02/2015 12:00:00 AM 14:00:00 Otro HERIDO
## 4 1470516 2015 01/02/2015 12:00:00 AM 17:15:00 Otro HERIDO
## 5 1470519 2015 01/02/2015 12:00:00 AM 10:30:00 Otro HERIDO
## 6 1470540 2015 01/02/2015 12:00:00 AM 18:40:00 Choque SOLO DAÑOS
## DIRECCION ZONA Diseño.Vía fecha
## 1 Con Tramo 15 COMUNA 6 Tramo de via 2015-01-01 07:15:00
## 2 Con Tramo 9 COMUNA 8 Tramo de via 2015-01-01 10:00:00
## 3 Con Tramo 17 Jurisdicción Lote o Predio 2015-01-02 14:00:00
## 4 Con Tramo 17 Jurisdicción Lote o Predio 2015-01-02 17:15:00
## 5 Tramo 16 Con Kilometro 5 Jurisdicción Lote o Predio 2015-01-02 10:30:00
## 6 Diagonal 75 B Con Calle 6 COMUNA 15 Tramo de via 2015-01-02 18:40:00
acc_moto1 <- acc_moto1 %>%
mutate(x = as.integer(str_extract(str_extract(DIRECCION, "Carrera [0-9]+"),
"[0-9]+")),
y = as.integer(str_extract(str_extract(DIRECCION, "Calle [0-9]+"),
"[0-9]+")),
es_sur = str_extract(acc_moto1$DIRECCION,
regex("sur", ignore_case = TRUE)),
y = ifelse(!is.na(es_sur), -y, y)) %>%
filter(x > 0 & x < 120,
y > - 23 & y < 120,
!is.na(x) & !is.na(y)) %>%
droplevels()
summary(acc_moto1)
## RADICADO AÑO_ACCIDENTE FECHA_ACCIDENTE HORA_ACCIDENTE CLASE_ACCIDENTE
## 1470349: 1 2015:25316 08/04/2017 12:00:00 AM: 139 17:00:00.000: 1672 : 3
## 1470356: 1 2016:26702 08/18/2015 12:00:00 AM: 126 18:00:00.000: 1643 Atropello :10598
## 1470357: 1 2017:25208 02/11/2016 12:00:00 AM: 122 07:00:00.000: 1555 Caida Ocupante:14511
## 1470426: 1 2018:23052 07/27/2016 12:00:00 AM: 115 16:00:00.000: 1520 Choque :60676
## 1470427: 1 2019:10927 03/17/2016 12:00:00 AM: 113 17:30:00.000: 1488 Incendio : 6
## 1470428: 1 10/07/2016 12:00:00 AM: 111 15:00:00.000: 1462 Otro :19320
## (Other):111199 (Other) :110479 (Other) :101865 Volcamiento : 6091
## GRAVEDAD_ACCIDENTE DIRECCION ZONA Diseño.Vía
## HERIDO :87964 Carrera 80 Con Calle 50: 317 COMUNA 10:18019 Tramo de via :76500
## MUERTO : 374 Carrera 80 Con Calle 65: 311 COMUNA 5 :11801 Interseccion :18295
## SOLO DAÑOS:22867 Carrera 80 Con Calle 80: 278 COMUNA 7 :10294 Lote o Predio:12546
## Carrera 65 Con Calle 80: 262 COMUNA 11:10193 Glorieta : 1997
## Carrera 57 Con Calle 44: 254 COMUNA 14:10152 Paso Elevado : 634
## Carrera 50 Con Calle 30: 247 COMUNA 16: 9040 Puente : 452
## (Other) :109536 (Other) :41706 (Other) : 781
## fecha x y es_sur
## Min. :2015-01-01 00:00:00 Min. : 1.00 Min. :-22.00 Length:111205
## 1st Qu.:2016-02-09 13:00:00 1st Qu.: 47.00 1st Qu.: 33.00 Class :character
## Median :2017-02-23 07:40:00 Median : 62.00 Median : 50.00 Mode :character
## Mean :2017-03-09 21:57:39 Mean : 59.63 Mean : 52.55
## 3rd Qu.:2018-04-10 07:30:00 3rd Qu.: 73.00 3rd Qu.: 75.00
## Max. :2019-06-30 23:00:00 Max. :119.00 Max. :119.00
##
acc_moto1 <- acc_moto1 %>%
mutate(COMUNA = as.numeric(str_extract(ZONA, "[0-9]+")))
library(readxl)
zona_comuna <- read_excel("zonas_comunas.xlsx")
acc_moto1 <- acc_moto1 %>%
left_join(zona_comuna, by = c("COMUNA" = "comuna"))
ggplot(acc_moto1, aes(x, y, col = GRAVEDAD_ACCIDENTE)) +
geom_point() +
scale_x_continuous(trans = "reverse")
ggplot(acc_moto1, aes(x, y)) +
geom_point() +
facet_wrap(~ nombre_comuna) +
scale_x_continuous(trans = "reverse")
¿Cuál es el comportamiento a lo largo de los años del número de accidentes por la gravedad del accidente?
t1 <- with(acc_moto1, table(year(fecha), GRAVEDAD_ACCIDENTE))
t1
## GRAVEDAD_ACCIDENTE
## HERIDO MUERTO SOLO DAÑOS
## 2015 20327 78 4911
## 2016 21350 85 5267
## 2017 19977 97 5134
## 2018 17955 77 5020
## 2019 8355 37 2535
# Guarda parámetros anteriores
param_ant <- par(no.readonly = TRUE)
par(mar = param_ant$mar + c(0, 3, 0, 0))
matplot(rownames(t1), t1, type = "l", las = 1,
xlim = c(2015, 2021),
xlab = "Año",
ylab = "", lwd = 3)
legend(2019.4, 15000, legend = colnames(t1),
col = 1:3, lty = 1)
mtext("Número de incidentes", side = 2, line = 4)
# Restaura parámetros anteriores.
par(param_ant)
t2 <- acc_moto1 %>%
group_by(año = year(fecha), GRAVEDAD_ACCIDENTE) %>%
summarise(num_acc = n())
t2
## # A tibble: 15 x 3
## # Groups: año [5]
## año GRAVEDAD_ACCIDENTE num_acc
## <dbl> <fct> <int>
## 1 2015 HERIDO 20327
## 2 2015 MUERTO 78
## 3 2015 SOLO DAÑOS 4911
## 4 2016 HERIDO 21350
## 5 2016 MUERTO 85
## 6 2016 SOLO DAÑOS 5267
## 7 2017 HERIDO 19977
## 8 2017 MUERTO 97
## 9 2017 SOLO DAÑOS 5134
## 10 2018 HERIDO 17955
## 11 2018 MUERTO 77
## 12 2018 SOLO DAÑOS 5020
## 13 2019 HERIDO 8355
## 14 2019 MUERTO 37
## 15 2019 SOLO DAÑOS 2535
ggplot(t2, aes(año, num_acc)) +
geom_line(aes(col = GRAVEDAD_ACCIDENTE), size = 1.5) +
labs(y = "Número de incidentes")