1 CARGA DE DATOS Y LIBRERIAS

CARGA DE LIBRERÍAS

library(readxl)
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)

CARGA DE DATOS

datos <- read_excel("D:/mining_dataset_final.xlsx")

# Verificamos columnas
names(datos)
##  [1] "Tipo_Roca"         "Zona"              "Metodo_Extraccion"
##  [4] "Empresa"           "Calidad_Mineral"   "Fragmentacion"    
##  [7] "Riesgo_Operativo"  "Fallas"            "Turno"            
## [10] "Equipos_Activos"   "Ley_%"             "Profundidad_m"    
## [13] "Recuperacion_%"    "Costo_USD_ton"     "Humedad_%"        
## [16] "Latitud"           "Longitud"
TipoRoca <- datos$Tipo_Roca

df_roca <- data.frame(variable = TipoRoca, stringsAsFactors = FALSE)
df_roca$grupo <- case_when(
  str_detect(df_roca$variable, regex("igne|igneous", ignore_case = TRUE)) ~ "IGNEA",
  str_detect(df_roca$variable, regex("sediment|sedimentary", ignore_case = TRUE)) ~ "SEDIMENTARIA",
  str_detect(df_roca$variable, regex("metamor|metamorphic", ignore_case = TRUE)) ~ "METAMORFICA",
  TRUE ~ NA_character_
)
TDF_roca <- df_roca %>%
  group_by(grupo) %>%
  summarise(ni = n(), .groups = "drop") %>%
  mutate(hi = round((ni / sum(ni)) * 100, 0))

TDF_roca_completo <- rbind(
  TDF_roca,
  data.frame(
    grupo = "TOTAL",
    ni = sum(TDF_roca$ni),
    hi = 100
  )
)

TDF_roca_completo
## # A tibble: 4 × 3
##   grupo           ni    hi
##   <chr>        <int> <dbl>
## 1 IGNEA          600    30
## 2 METAMORFICA    400    20
## 3 SEDIMENTARIA  1000    50
## 4 TOTAL         2000   100
tabla_roca <- TDF_roca_completo %>%
  gt() %>%
  tab_header(
    title = "Tabla N° 2",
    subtitle = "Distribución de los Principales Tipos de Roca"
  ) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(rows = grupo == "TOTAL")
  )

tabla_roca
Tabla N° 2
Distribución de los Principales Tipos de Roca
grupo ni hi
IGNEA 600 30
METAMORFICA 400 20
SEDIMENTARIA 1000 50
TOTAL 2000 100
barplot(TDF_roca$ni,
        main = "Frecuencia por Tipo de Roca",
        col = "steelblue",
        names.arg = TDF_roca$grupo,
        las = 1)

barplot(TDF_roca$hi,
        main = "Porcentaje por Tipo de Roca",
        col = "steelblue",
        names.arg = TDF_roca$grupo,
        las = 1,
        ylim = c(0,100))

par(mar = c(4, 4, 4, 8))

colores <- c("orange", "skyblue", "purple")

pie(TDF_roca$hi,
    col = colores,
    main = "Distribución de Tipos de Roca",
    labels = NA)

legend("right",
       legend = paste(TDF_roca$grupo, TDF_roca$hi, "%"),
       fill = colores,
       title = "GRUPOS",
       bty = "o",
       xpd = TRUE,
       inset = c(-0.20, 0))

moda_roca <- TDF_roca[TDF_roca$ni == max(TDF_roca$ni), ]
moda_roca
## # A tibble: 1 × 3
##   grupo           ni    hi
##   <chr>        <int> <dbl>
## 1 SEDIMENTARIA  1000    50