1 LECTURA DE DATOS

# 1. LIBRERIAS Y CARGA DE DATOS
library(readxl)
library(dplyr)
library(gt)
library(readr)

setwd("C:/Users/veru2/OneDrive/Escritorio/dataset_excel")
Datos <- read.csv("Oil__Gas____Other_Regulated_Wells__Beginning_1860 (2).csv",
                  header = TRUE, sep = ";", dec = ",")

2 TABLA DE DISTRIBUCION DE FRECUENCIA

Municipios <- Datos$Town
TDFMunicipios <- as.data.frame(table(Municipios))

TDFMunicipios <- TDFMunicipios[TDFMunicipios$Municipios != "", ]

nrow(TDFMunicipios)                                       
## [1] 523
head(TDFMunicipios[order(-TDFMunicipios$Freq), ], 10)    
##     Municipios Freq
## 11    Allegany 6088
## 48     Bolivar 5422
## 13        Alma 3621
## 82  Carrollton 2724
## 517       Wirt 1537
## 336      Olean 1492
## 177    Genesee 1405
## 415       Scio 1341
## 63       Busti  966
## 505 West Union  926

3 ASIGNACION DE CONDADOS

Cada municipio pertenece a un condado. Como son muchos municipios (demasiados para una grafica), los agrupamos en los 7 condados con mas pozos y el resto en “Otros”. Primero obtenemos el condado de cada municipio desde los datos.

muni_condado <- Datos %>%
  filter(Town != "", County != "") %>%
  count(Town, County, name = "n") %>%
  group_by(Town) %>%
  slice_max(order_by = n, n = 1, with_ties = FALSE) %>% 
  ungroup() %>%
  select(Town, County)

TDFMunicipios <- merge(TDFMunicipios, muni_condado,
                       by.x = "Municipios", by.y = "Town", all.x = TRUE)

condados_top <- c("Allegany", "Cattaraugus", "Chautauqua", "Erie",
                  "Steuben", "Wyoming", "Genesee")
TDFMunicipios$Condado <- ifelse(TDFMunicipios$County %in% condados_top,
                                TDFMunicipios$County, "Otros")

TDFMunicipios <- TDFMunicipios[, c("Municipios", "Freq", "County", "Condado")]
head(TDFMunicipios)
##   Municipios Freq    County Condado
## 1      Adams    3 Jefferson   Otros
## 2    Addison    5   Steuben Steuben
## 3      Afton    7  Chenango   Otros
## 4    Alabama   34   Genesee Genesee
## 5     Albany    1    Albany   Otros
## 6     Albion    2   Orleans   Otros

4 CALCULO DE FRECUENCIAS POR CONDADO

Frecuencia absoluta (ni) y relativa porcentual (hi) de pozos por condado.

TDFMunicipios$Freq <- as.numeric(as.character(TDFMunicipios$Freq))

TDFMunicipiosfinal1 <- TDFMunicipios %>%
  group_by(Condado) %>%
  summarise(
    ni = sum(Freq),
    hi = round(sum(Freq) / sum(TDFMunicipios$Freq) * 100, 5)) %>%
  arrange(desc(ni)) %>%
  as.data.frame()

# Mover "Otros" al final de la tabla
TDFMunicipiosfinal1 <- rbind(TDFMunicipiosfinal1[TDFMunicipiosfinal1$Condado != "Otros", ],
                             TDFMunicipiosfinal1[TDFMunicipiosfinal1$Condado == "Otros", ])

5 CONSTRUCCION DEL CUADRO CON TOTALES

TDFMunicipiosfinal1 <- TDFMunicipiosfinal1[, c("Condado", "ni", "hi")]
total_ni <- sum(TDFMunicipiosfinal1$ni)
total_hi <- sum(TDFMunicipiosfinal1$hi)
TDFMunicipioscompleta <- rbind(TDFMunicipiosfinal1,
                               data.frame(Condado = "Total",
                                          ni = total_ni,
                                          hi = total_hi))
print(TDFMunicipioscompleta)
##        Condado    ni        hi
## 1     Allegany 16397  35.06405
## 2  Cattaraugus 11945  25.54370
## 3   Chautauqua  6367  13.61547
## 5         Erie  3485   7.45247
## 6      Steuben  2067   4.42016
## 7      Wyoming  1149   2.45707
## 8      Genesee   880   1.88183
## 4        Otros  4473   9.56525
## 11       Total 46763 100.00000

6 PRESENTACION TABULAR

gt(TDFMunicipioscompleta) %>%
  tab_header(
    title = md("**DISTRIBUCION DE FRECUENCIAS DE POZOS POR CONDADO - NUEVA YORK**"),
    subtitle = "Distribucion de pozos de petroleo y gas segun el condado") %>%
  fmt_number(
    columns = hi,
    decimals = 2) %>%
  cols_align(align = "center", columns = everything()) %>%
  tab_style(
    style = list(cell_fill(color = "#2E4053"), cell_text(color = "white", weight = "bold")),
    locations = cells_title()
  ) %>%
  tab_style(
    style = list(cell_fill(color = "#F2F3F4"), cell_text(weight = "bold", color = "#2E4053")),
    locations = cells_column_labels()
  ) %>%
  tab_options(
    table.border.top.color = "#2E4053",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6))
DISTRIBUCION DE FRECUENCIAS DE POZOS POR CONDADO - NUEVA YORK
Distribucion de pozos de petroleo y gas segun el condado
Condado ni hi
Allegany 16397 35.06
Cattaraugus 11945 25.54
Chautauqua 6367 13.62
Erie 3485 7.45
Steuben 2067 4.42
Wyoming 1149 2.46
Genesee 880 1.88
Otros 4473 9.57
Total 46763 100.00

7 GRAFICAS

7.1 Histograma de frecuencia absoluta local

datos <- TDFMunicipioscompleta[TDFMunicipioscompleta$Condado != "Total", ]

bp <- barplot(datos$ni,
        main = "Grafica N1: Distribucion de pozos segun su condado en Nueva York",
        ylab = "Cantidad de pozos", col = "#2E4053",
        names.arg = datos$Condado,
        las = 2, cex.names = 0.9, cex.axis = 0.8, cex.main = 0.95,
        ylim = c(0, max(datos$ni) * 1.15))
text(x = bp, y = datos$ni,
     labels = format(datos$ni, big.mark = ","),
     pos = 3, cex = 0.7, xpd = TRUE)
mtext("Condado", side = 1, line = 6.5, cex = 0.95)

7.2 Histograma de frecuencia absoluta global

bp <- barplot(datos$ni,
        main = "Grafica N2: Cantidad de pozos segun su condado en Nueva York",
        ylab = "Cantidad de pozos", col = "#2E4053",
        names.arg = datos$Condado,
        las = 2, cex.names = 0.9, cex.axis = 0.8, cex.main = 0.95,
        ylim = c(0, max(datos$ni) * 1.15))
text(x = bp, y = datos$ni,
     labels = format(datos$ni, big.mark = ","),
     pos = 3, cex = 0.7, xpd = TRUE)
mtext("Condado", side = 1, line = 6.5, cex = 0.95)

7.3 Histograma de frecuencia relativa local

bp <- barplot(datos$hi,
        main = "Grafica N3: Distribucion en porcentaje segun su condado en Nueva York",
        ylab = "Porcentaje (%)", col = "#2E4053",
        names.arg = datos$Condado,
        las = 2, cex.names = 0.9, cex.axis = 0.8, cex.main = 0.95,
        ylim = c(0, max(datos$hi) * 1.15))
text(x = bp, y = datos$hi,
     labels = paste0(round(datos$hi, 2), "%"),
     pos = 3, cex = 0.7, xpd = TRUE)
mtext("Condado", side = 1, line = 6.5, cex = 0.95)

7.4 Histograma de frecuencia relativa global

bp <- barplot(datos$hi,
        main = "Grafica N4: Distribucion en porcentaje segun su condado en Nueva York",
        ylab = "Porcentaje (%)", col = "#2E4053",
        names.arg = datos$Condado,
        las = 2, cex.names = 0.9, cex.axis = 0.8, cex.main = 0.95,
        ylim = c(0, 100))
text(x = bp, y = datos$hi,
     labels = paste0(round(datos$hi, 2), "%"),
     pos = 3, cex = 0.7, xpd = TRUE)
mtext("Condado", side = 1, line = 6.5, cex = 0.95)

7.5 Diagrama circular

par(mar = c(2, 2, 4, 9), xpd = TRUE)
colores <- c("#1B4F72", "#2874A6", "#3498DB", "#5DADE2",
             "#85C1E9", "#AED6F1", "#D6EAF8", "#BFC9CA")
colores <- colores[1:nrow(datos)]

pct <- round(datos$hi, 2)
pie(datos$ni, labels = "",
    col = colores, border = "white", radius = 0.95,
    init.angle = 90, clockwise = TRUE,
    main = "Grafica N5: Distribucion porcentual de pozos por condado en Nueva York")

frac <- datos$ni / sum(datos$ni)
theta <- (90 - 360 * (cumsum(frac) - frac / 2)) * pi / 180
text(0.6 * cos(theta), 0.6 * sin(theta),
     labels = ifelse(pct >= 4, paste0(pct, "%"), ""),
     col = "white", cex = 0.85, font = 2)

legend(x = 1.05, y = 0.9,
       legend = paste0(datos$Condado, "  (", pct, "%)"),
       fill = colores, cex = 0.8, bty = "n", title = "Condado")

8 RESUMEN ESTADISTICO

La variable analizada (condado) es cualitativa nominal, por lo que los estadisticos de tendencia central y dispersion (media, varianza, asimetria, etc.) no aplican. El unico estadistico valido es la moda.

Conclusiones <- data.frame(
  Variable = "Ubicacion por condado",
  `Rango [Min; Max]` = "N/A",
  `Media (X)` = "N/A",
  `Mediana (Me)` = "N/A",
  `Moda (Mo)` = "Allegany",
  `Varianza (S2)` = "N/A",
  `Desv. Est. (S)` = "N/A",
  `C.V. (%)` = "N/A",
  `Asimetria (As)` = "N/A",
  `Curtosis (K)` = "N/A",
  `Valores Atipicos` = "N/A",
  check.names = FALSE
)

gt(Conclusiones) %>%
  tab_header(
    title = md("**CONCLUSIONES Y ESTADISTICOS**"),
    subtitle = "Resumen de indicadores de ubicacion de los pozos por condado en Nueva York") %>%
  tab_source_note(source_note = "Autor: Dallyana Lozano") %>%
  cols_align(align = "center", columns = everything()) %>%
  tab_style(
    style = list(cell_fill(color = "#2E4053"), cell_text(color = "white", weight = "bold")),
    locations = cells_title()
  ) %>%
  tab_style(
    style = list(cell_fill(color = "#F2F3F4"), cell_text(weight = "bold", color = "#2E4053")),
    locations = cells_column_labels()
  ) %>%
  tab_options(
    table.border.top.color = "#2E4053",
    table.border.bottom.color = "#2E4053",
    column_labels.border.bottom.color = "#2E4053",
    data_row.padding = px(6))
CONCLUSIONES Y ESTADISTICOS
Resumen de indicadores de ubicacion de los pozos por condado en Nueva York
Variable Rango [Min; Max] Media (X) Mediana (Me) Moda (Mo) Varianza (S2) Desv. Est. (S) C.V. (%) Asimetria (As) Curtosis (K) Valores Atipicos
Ubicacion por condado N/A N/A N/A Allegany N/A N/A N/A N/A N/A N/A
Autor: Dallyana Lozano

8.1 Conclusiones

La moda corresponde al condado de Allegany, que concentra la mayor cantidad de pozos del estado (35%), seguido de Cattaraugus (26%) y Chautauqua (14%). Esto evidencia que la actividad petrolera y gasifera de Nueva York se concentra historicamente en el oeste del estado (Southern Tier).