CARGA DE DATOS

Verificamos que rstudio nos lea correctamente los datos

datos <- read.csv("Datos Mineros.csv",
                  header = TRUE,
                  sep = ";",
                  dec = ".",
                  fileEncoding = "latin1")

EXTRAER VARIABLE

CAUSAS_DEL_DAÑO <- datos$INJURY_SOURCE

1.2 Cargamos Librerias

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
library(stringr)
library(gt)

2 Tabla de Distribución De Frecuencias Simples

# Convertimos a dataframe
df_causas <- data.frame(variable = CAUSAS_DEL_DAÑO)

Agrupamos por palabras claves ya que los datos eran muy extensos y al momento de hacer las Tablas de Frecuencia se realiza una tabla extensa

#  AGRUPACIÓN POR PALABRAS CLAVE
df_causas$grupo <- case_when(
  str_detect(df_causas$variable, regex("NO VALUE", ignore_case = TRUE)) ~ "SIN VALOR",
  str_detect(df_causas$variable, regex("METAL", ignore_case = TRUE)) ~ "METALES / PIEZAS",
  str_detect(df_causas$variable, regex("GROUND|ROCK|COAL|ORE|CAVING", ignore_case = TRUE)) ~ "ROCA / MINERAL",
  str_detect(df_causas$variable, regex("MINE FLOOR|BOTTOM|FOOTWAL", ignore_case = TRUE)) ~ "PISO / SUPERFICIE",
  str_detect(df_causas$variable, regex("MACHINES", ignore_case = TRUE)) ~ "MAQUINARIA / EQUIPO",
  str_detect(df_causas$variable, regex("DUST", ignore_case = TRUE)) ~ "POLVO / MINERAL MOLIDO",
  TRUE ~ "TRANSPORTADORES DE BANDA/MOTORES"
)

2.1 Calculamos las frecuencias absolutas y relativas simples

TDFgrupoCausas <- df_causas %>%
  group_by(grupo) %>%
  summarise(ni = n()) %>%
  mutate(hi = round((ni / sum(ni)) * 100, 0)) %>%
  arrange(desc(ni))  # Orden descendente por frecuencia

# Agregar totales
total_ni <- sum(TDFgrupoCausas$ni)
total_hi <- 100

TDFgrupoCausasCompleto <- rbind(
  TDFgrupoCausas,
  data.frame(grupo = "TOTAL", ni = total_ni, hi = total_hi)
)

print(TDFgrupoCausasCompleto)
## # A tibble: 8 × 3
##   grupo                               ni    hi
##   <chr>                            <int> <dbl>
## 1 TRANSPORTADORES DE BANDA/MOTORES  2088    42
## 2 ROCA / MINERAL                     916    19
## 3 METALES / PIEZAS                   855    17
## 4 SIN VALOR                          591    12
## 5 MAQUINARIA / EQUIPO                202     4
## 6 PISO / SUPERFICIE                  163     3
## 7 POLVO / MINERAL MOLIDO             130     3
## 8 TOTAL                             4945   100

2.2 Tabla completa

tabla_causas_grupo <- TDFgrupoCausasCompleto %>%
  gt() %>%
  tab_header(
    title = md("Tabla N° 1"),
    subtitle = md("Distribución de Frecuencias Agrupadas de INJURY_SOURCE")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo 1")
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    column_labels.border.top.color = "black",
    column_labels.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2),
    row.striping.include_table_body = TRUE,
    heading.border.bottom.color = "black",
    heading.border.bottom.width = px(2),
    table_body.hlines.color = "gray",
    table_body.border.bottom.color = "black"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(
      rows = grupo == "TOTAL"
    )
  )

tabla_causas_grupo
Tabla N° 1
Distribución de Frecuencias Agrupadas de INJURY_SOURCE
grupo ni hi
TRANSPORTADORES DE BANDA/MOTORES 2088 42
ROCA / MINERAL 916 19
METALES / PIEZAS 855 17
SIN VALOR 591 12
MAQUINARIA / EQUIPO 202 4
PISO / SUPERFICIE 163 3
POLVO / MINERAL MOLIDO 130 3
TOTAL 4945 100
Autor: Grupo 1
#Comprobación del tamaño
sum(TDFgrupoCausas$ni)
## [1] 4945
sum(TDFgrupoCausas$hi)
## [1] 100

3 Gráficas de distribución de frecuencia

3.1 Diagrama de barras ni

barplot(TDFgrupoCausas$ni,
        main = "Gráfica Nº1:Frecuencia de FUENTE DE LA LESIÓN por Grupo (Local)",
        xlab = "Grupo",
        ylab = "Cantidad (ni)",
        col = "steelblue",
        names.arg = TDFgrupoCausas$grupo,
        las = 2,
        cex.names = 0.4)

barplot(TDFgrupoCausas$ni,
        main = "Gráfica Nº2: Frecuencia de FUENTE DE LA LESIÓN por Grupo (Global)",
        xlab = "Grupo de INJURY_SOURCE",
        ylab = "Cantidad (ni)",
        col = "steelblue",
        names.arg = TDFgrupoCausas$grupo,
        cex.names = 0.4,  
        las = 2,          
        ylim = c(0, max(TDFgrupoCausas$ni) * 1.2))  

3.2 Diagrama de barras hi

barplot(TDFgrupoCausas$hi,
        main = "Gráfica Nº3: Porcentaje de FUENTE DE LA LESIÓN por Grupo",
        xlab = "Grupo",
        ylab = "Porcentaje (%)",
        col = "steelblue",
        names.arg = TDFgrupoCausas$grupo,
        las = 2,
        cex.names = 0.4,
        ylim = c(0, max(TDFgrupoCausas$hi)*1.2))

barplot(TDFgrupoCausas$hi,
        main = "Gráfica Nº4: Porcentaje de FUENTE DE LA LESIÓN por Grupo (Global)",
        xlab = "Grupo de INJURY_SOURCE",
        ylab = "Porcentaje (%)",
        col = "steelblue",
        names.arg = TDFgrupoCausas$grupo,
        cex.names = 0.4,  
        las = 2,          
        ylim = c(0, 100))

3.2 Diagrama de barras hi

Colores <- colorRampPalette(c("lightskyblue", "darkblue"))

etiquetas <- paste(TDFgrupoCausas$hi, "%", sep="")

pie(TDFgrupoCausas$hi, 
    radius = 0.6, 
    col = Colores(nrow(TDFgrupoCausas)),  # Número de colores según la cantidad de grupos
    main = "Gráfica Nº5: Porcentaje de INJURY_SOURCE por Grupo", 
    labels = etiquetas)

legend(x = 0.90, y = -0.3,  # Ajusta estos valores según el gráfico
       title = "Grupos",
       legend = TDFgrupoCausas$grupo,
       fill = Colores(nrow(TDFgrupoCausas)),
       cex = 0.380,
       title.cex = 1)

4 Indicadores

moda_CAUSAS_DEL_DAÑO <- TDFgrupoCausas[TDFgrupoCausas$ni == max(TDFgrupoCausas$ni), ]
print(moda_CAUSAS_DEL_DAÑO)
## # A tibble: 1 × 3
##   grupo                               ni    hi
##   <chr>                            <int> <dbl>
## 1 TRANSPORTADORES DE BANDA/MOTORES  2088    42
'1 TRANSPORTADORES DE BANDA/MOTORES  2088    42'
## [1] "1 TRANSPORTADORES DE BANDA/MOTORES  2088    42"