#---------------------CARGA DE DATOS---------------------#

# cargar librerías
library(readxl)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(gt)

#cargar datos
datos <- read.csv("C:\\Users\\joeja\\Desktop\\Proyecto Estadística\\Depositos_sulfuro.csv", 
                  header = TRUE, 
                  sep = ";", 
                  dec = ".")

#mostrar datos
datos <- datos %>%
  mutate(
    postmindef = trimws(postmindef),
    postmindef = case_when(
      postmindef %in% c("yee", "Yes", "yes") ~ "YES",
      postmindef %in% c("no", "No", "NO") ~ "NO",
      postmindef == "" | is.na(postmindef) ~ "Sin Registro",
      TRUE ~ postmindef
    )
  )

#cargar variable
datos$postmindef <- trimws(datos$postmindef)
Deformacion <- datos$postmindef

#Crear tabla de frecuencia y Convertir tabla en dataframe
TDFDeformacion <- table(Deformacion)
TDFDeformacion <- as.data.frame(TDFDeformacion)
TDFDeformacion
##    Deformacion Freq
## 1           NO   33
## 2 Sin Registro  188
## 3          YES  869
#calcular frecuencia absolutas y simples
TDFDeformacionFinal <- TDFDeformacion %>%
  group_by(Deformacion) %>% 
  summarise(
    ni = sum(Freq),
    hi = round((sum(ni) / sum(TDFDeformacion$Freq)) * 100, 2)
  )

TDFDeformacionFinal <- data.frame(TDFDeformacionFinal)
TDFDeformacionFinal
##    Deformacion  ni    hi
## 1           NO  33  3.03
## 2 Sin Registro 188 17.25
## 3          YES 869 79.72
#Comprobación del tamaño
sum(TDFDeformacionFinal$ni)
## [1] 1090
sum(TDFDeformacionFinal$hi)
## [1] 100
#Agregar Total
Total_ni <- sum(TDFDeformacionFinal$ni)
Total_hi <- 100

TDFDeformacionFinalCompleto <- rbind(TDFDeformacionFinal,
                       data.frame(Deformacion="Total", ni = Total_ni, hi = Total_hi))
print(TDFDeformacionFinalCompleto)
##    Deformacion   ni     hi
## 1           NO   33   3.03
## 2 Sin Registro  188  17.25
## 3          YES  869  79.72
## 4        Total 1090 100.00
#Crear Tabla
TablaDeformacion <- TDFDeformacionFinalCompleto %>%
  gt(rowname_col = NULL) %>%
  tab_header(
    title = md("**Tabla N°**"),
    subtitle = md("**Tabla de Distribucion de Frecuencias Absolutas y Relativas Simples
                  de la Deformación Post Mineralización**")
  ) %>%
  tab_source_note(
    source_note = md("Autor: Grupo2")
  ) %>%
  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 = TDFDeformacionFinalCompleto$Deformacion == "Total"
    )
  ) 

TablaDeformacion
Tabla N°
Tabla de Distribucion de Frecuencias Absolutas y Relativas Simples de la Deformación Post Mineralización
Deformacion ni hi
NO 33 3.03
Sin Registro 188 17.25
YES 869 79.72
Total 1090 100.00
Autor: Grupo2