title:“Prueba Ministerio de Defensa”
output:html_document
date:“2025-12-31”

R Markdown

  1. Usando como fuente de información la base de datos de homicidios del Observatorio de Derechos Humanos y Defensa Nacional (adjunta en el correo), realice una comparación de los homicidios ocurridos en 2023 y 2024. Incluya datos sobre la tasa de homicidios por cada cien mil habitantes. Presente la variación tanto a nivel departamental como municipal, mostrando un ranking de los lugares más afectados en cada caso.

Respuesta

Como primer paso se abré la base de datos proporcionada,pasamos analizar los datos comparando los homicidios totales entre los años 2023 y 2024,creando después una tabla de comparación con su respectiva variación

#Librerias

library(readxl)
Base_Datos_Homicidio_14_02_2025 <- read_excel("~/Downloads/Base Datos Homicidio 14,02.2025.xlsx")
head(Base_Datos_Homicidio_14_02_2025)
## # A tibble: 6 × 8
##   FECHA_HECHO         COD_DEPTO DEPARTAMENTO COD_MUNI MUNICIPIO ZONA   SEXO     
##   <dttm>              <chr>     <chr>        <chr>    <chr>     <chr>  <chr>    
## 1 2003-01-01 00:00:00 91        AMAZONAS     91001    LETICIA   RURAL  MASCULINO
## 2 2003-01-01 00:00:00 05        ANTIOQUIA    05034    ANDES     RURAL  MASCULINO
## 3 2003-01-01 00:00:00 05        ANTIOQUIA    05045    APARTADO  URBANA MASCULINO
## 4 2003-01-01 00:00:00 05        ANTIOQUIA    05088    BELLO     URBANA MASCULINO
## 5 2003-01-01 00:00:00 05        ANTIOQUIA    05088    BELLO     URBANA MASCULINO
## 6 2003-01-01 00:00:00 05        ANTIOQUIA    05088    BELLO     URBANA MASCULINO
## # ℹ 1 more variable: VICTIMAS <dbl>
options(repos = c(CRAN = "https://cran.rstudio.com/"))
install.packages("ggplot2")
## 
##   There is a binary version available but the source version is later:
##         binary source needs_compilation
## ggplot2  3.4.4  4.0.1             FALSE
install.packages("readxl")
## 
##   There is a binary version available but the source version is later:
##        binary source needs_compilation
## readxl  1.4.3  1.4.5              TRUE
library(readxl)
library(ggplot2)
install.packages("plotly")
## 
##   There is a binary version available but the source version is later:
##        binary source needs_compilation
## plotly 4.10.3 4.11.0             FALSE
library(plotly)
install.packages("datarium")
## 
## The downloaded binary packages are in
##  /var/folders/gt/5byd18wn2sn_cfms0jl_95f00000gn/T//RtmpzVKKty/downloaded_packages
library(datarium)
install.packages("dplyr")
## 
## The downloaded binary packages are in
##  /var/folders/gt/5byd18wn2sn_cfms0jl_95f00000gn/T//RtmpzVKKty/downloaded_packages
library(dplyr)
install.packages("lubridate")
## 
##   There is a binary version available but the source version is later:
##           binary source needs_compilation
## lubridate  1.9.3  1.9.4              TRUE
library(lubridate)
total_victimas_2024 <- sum(
  Base_Datos_Homicidio_14_02_2025$VICTIMAS[format(Base_Datos_Homicidio_14_02_2025$`FECHA_HECHO`, "%Y") == "2024"],
  na.rm = TRUE
)

total_victimas_2024
## [1] 13357
total_victimas_2023 <- sum(
  Base_Datos_Homicidio_14_02_2025$VICTIMAS[
    format(Base_Datos_Homicidio_14_02_2025$FECHA_HECHO, "%Y") == "2023"
  ],
  na.rm = TRUE
)



tabla_comparacion <- data.frame(
  Año = c(2023, 2024),
  Total_Homicidios = c(total_victimas_2023, total_victimas_2024)
)

tabla_comparacion$Diferencia <- c(
  NA,
  tabla_comparacion$Total_Homicidios[2] - tabla_comparacion$Total_Homicidios[1]
)

tabla_comparacion$Variacion_Porcentual <- c(
  NA,
  round(
    (tabla_comparacion$Total_Homicidios[2] -
       tabla_comparacion$Total_Homicidios[1]) /
      tabla_comparacion$Total_Homicidios[1] * 100,
    2
  )
)

tabla_comparacion
##    Año Total_Homicidios Diferencia Variacion_Porcentual
## 1 2023            13555         NA                   NA
## 2 2024            13357       -198                -1.46
library(dplyr)

Total_poblacion <- data.frame(
  Año = c(2023, 2024),
  PoblacionTotal = c(52314000, 52695952)  
)


install.packages("dplyr")
## 
## The downloaded binary packages are in
##  /var/folders/gt/5byd18wn2sn_cfms0jl_95f00000gn/T//RtmpzVKKty/downloaded_packages
library(dplyr)

datos_tasa <- tabla_comparacion %>%   
  left_join(Total_poblacion, by = "Año") %>%
  mutate(Tasa_Homicidios = (Total_Homicidios / PoblacionTotal) * 100000)


library(knitr)
kable(datos_tasa, caption = "Tasa de homicidios por 100.000 habitantes")
Tasa de homicidios por 100.000 habitantes
Año Total_Homicidios Diferencia Variacion_Porcentual PoblacionTotal Tasa_Homicidios
2023 13555 NA NA 52314000 25.91085
2024 13357 -198 -1.46 52695952 25.34730
install.packages("ggplot2", type = "binary")
## 
##   There is a binary version available (and will be installed) but the
##   source version is later:
##         binary source
## ggplot2  3.4.4  4.0.1
## 
## 
## The downloaded binary packages are in
##  /var/folders/gt/5byd18wn2sn_cfms0jl_95f00000gn/T//RtmpzVKKty/downloaded_packages
library(ggplot2)
ggplot(datos_tasa, aes(x = factor(Año), y = Tasa_Homicidios)) +
  geom_bar(stat = "identity", fill = "steelblue", width = 0.4) +  
  labs(
    title = "Tasa de homicidios por 100.000 habitantes",
    x = "Año",
    y = "Tasa de homicidios"
  ) +
  theme_minimal()

Se puede observar que entre al año 2023 y 2024 hubo una disminución de homicidos de 198 con una tasa de variación de 1.46%.

Ahora para la variación de los departamentos se filtran el total de homicidios por el total de los departamentos.

departamentos <- Base_Datos_Homicidio_14_02_2025 %>%
  mutate(AÑO = year(FECHA_HECHO)) %>%  # extrae el año de la fecha
  filter(AÑO %in% c(2023, 2024)) %>%  # solo años 2023 y 2024
  select(DEPARTAMENTO, VICTIMAS, FECHA_HECHO)

Ahora para la variación de los departamentos se filtran el total de homicidios por todos los departamentos.

Gráfico

library(ggplot2)

library(ggplot2)


victimas_anuales <- departamentos %>%
  mutate(AÑO = year(FECHA_HECHO)) %>%
  filter(AÑO %in% c(2023, 2024)) %>%
  group_by(DEPARTAMENTO, AÑO) %>%
  summarise(Total_Victimas = sum(VICTIMAS, na.rm = TRUE), .groups = "drop")


ggplot(victimas_anuales, aes(x = AÑO, y = Total_Victimas, group = DEPARTAMENTO, color = DEPARTAMENTO)) +
  geom_line(size = 1) +       # línea por departamento
  geom_point(size = 3) +      # puntos para cada año
  labs(
    title = "Número de víctimas por departamento (2023 vs 2024)",
    x = "Año",
    y = "Número de víctimas",
    color = "Departamento"
  ) +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Se puede evidenciar claramenrte que el departamento con mayor numero de homicidios es el valle del cauca seguido por Antioquia y en tercer lugar Bogotá.

Homicidios realizados encontra de mujeres

library(dplyr)
library(lubridate)

victimas_mujeres <- Base_Datos_Homicidio_14_02_2025 %>%
  filter(SEXO == "FEMENINO") %>%
  select(FECHA_HECHO, VICTIMAS)

victimas_por_año <- victimas_mujeres %>%
  mutate(AÑO = year(FECHA_HECHO)) %>%
  filter(AÑO %in% c(2023, 2024)) %>%
  group_by(AÑO) %>%
  summarise(Total_Victimas = sum(VICTIMAS, na.rm = TRUE), .groups = "drop") %>%
  arrange(AÑO)

poblacion_mujeres <- c(`2023` = 26497041, `2024` = 26980327)

tasa_mujeres <- victimas_por_año %>%
  mutate(
    POBLACION_MUJERES = poblacion_mujeres[as.character(AÑO)],
    Tasa_Homicidios = (Total_Victimas / POBLACION_MUJERES) * 100000
  )

tasa_mujeres
## # A tibble: 2 × 4
##     AÑO Total_Victimas POBLACION_MUJERES Tasa_Homicidios
##   <dbl>          <dbl>             <dbl>           <dbl>
## 1  2023            971          26497041            3.66
## 2  2024            969          26980327            3.59

En los homicidios presentados en mujeres se puede observar que no hay diferencias significativas en el total de casos en el 2023 vs 2024. La diferencia es solo de dos casos.