0.- Librerias

library(readr)
library(dplyr)
library(knitr)
library(kableExtra)
library(ggplot2)

1.- leer datos

setwd("C:/Users/CESAR/Downloads/proyecto x")
variables <- read_csv("GlobalweatherRepository.csv", show_col_types = TRUE)

2.- Depuración y selección de la variable

Depuración

variables <- na.omit(variables)

Selección

Condicion_climatica <- variables$condition_text

3.- Frecuencias

Tabla 1

Tabla_Condicion_climatica <- table(Condicion_climatica) 
ni_Condicion_climatica <- as.vector(Tabla_Condicion_climatica) 
hi_Condicion_climatica <- round((ni_Condicion_climatica / sum(ni_Condicion_climatica)) * 100, 2)

Tabla simplificada

variables$Condicion_Climatica_Limpia <- case_when(
  grepl("Clear|Sunny", variables$condition_text, ignore.case = TRUE) ~ "Despejado / Soleado",
  grepl("Cloudy|Overcast|Partly Cloudy", variables$condition_text, ignore.case = TRUE) ~ "Nublado/Parcialmente Nublado",
  grepl("Mist|Fog", variables$condition_text, ignore.case = TRUE) ~ "Neblina / Niebla",
  grepl("Rain|Drizzle|Shower", variables$condition_text, ignore.case = TRUE) ~ "Lluvioso / Chubascos",
  grepl("Snow|Sleet|Ice|Hail", variables$condition_text, ignore.case = TRUE) ~ "Nieve / Granizo",
  grepl("Thunder|Thundery", variables$condition_text, ignore.case = TRUE) ~ "Tormenta Electrica",
  TRUE ~ "Otros fenomeno local "
)

tabla_base_clima <- variables %>%
  group_by(Condicion_Climatica_Limpia) %>%  
  summarise(
    ni = n(),
    hi = round((n() / nrow(variables)) * 100, 2),
    .groups = "drop"
  ) %>%
  arrange(desc(ni))

4.- Tabla de frecuencia

TDF_Condicion_Climatica_Base <- data.frame(
  Condicion_Climatica = names(Tabla_Condicion_climatica),
  ni = as.numeric(ni_Condicion_climatica),
  hi = as.numeric(hi_Condicion_climatica),
  stringsAsFactors = FALSE
)
sumatoria_condicion_climatica <- data.frame(
  Condicion_Climatica = "TOTAL",
  ni = sum(TDF_Condicion_Climatica_Base$ni, na.rm = TRUE),
  hi = 100
)
TDF_Condicion_Climatica_Suma <- rbind(TDF_Condicion_Climatica_Base, sumatoria_condicion_climatica)

colnames(TDF_Condicion_Climatica_Suma) <- c(
  "Condicion Climatica", 
  "Frecuencia Absoluta", 
  "Frecuencia Relativa"
)

kable(TDF_Condicion_Climatica_Suma, align = "c", row.names = FALSE,
      caption = "Tabla N.1: Distribucion de frecuencia para la variable Condicion Climatica en capitales del mundo para el análisis meteorológico del periodo mayo del 2024 a mayo del 2026 ") %>%
  kable_styling(full_width = TRUE, position = "center",
                bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
  row_spec(0, bold = TRUE, color = "white", background = "#2C3E50") %>%
  row_spec(nrow(TDF_Condicion_Climatica_Suma), bold = TRUE, background = "#EAEDED") %>%
  footnote(general = "Fuente: GlobalWeatherRepository. Grupo 2.",
           footnote_as_chunk = TRUE)
Tabla N.1: Distribucion de frecuencia para la variable Condicion Climatica en capitales del mundo para el análisis meteorológico del periodo mayo del 2024 a mayo del 2026
Condicion Climatica Frecuencia Absoluta Frecuencia Relativa
Blizzard 10 0.01
Blowing snow 41 0.03
Clear 7601 5.36
Cloudy 1711 1.21
Fog 1757 1.24
Freezing drizzle 5 0.00
Freezing fog 248 0.18
Heavy freezing drizzle 1 0.00
Heavy rain 63 0.04
Heavy rain at times 14 0.01
Heavy snow 129 0.09
Light drizzle 708 0.50
Light freezing rain 93 0.07
Light rain 4434 3.13
Light rain shower 3230 2.28
Light sleet 62 0.04
Light sleet showers 12 0.01
Light snow 525 0.37
Light snow showers 64 0.05
Mist 5908 4.17
Moderate or heavy freezing rain 2 0.00
Moderate or heavy rain in area with thunder 11 0.01
Moderate or heavy rain shower 293 0.21
Moderate or heavy rain with thunder 1133 0.80
Moderate or heavy sleet 6 0.00
Moderate or heavy snow in area with thunder 4 0.00
Moderate or heavy snow showers 52 0.04
Moderate rain 1068 0.75
Moderate rain at times 129 0.09
Moderate snow 87 0.06
Overcast 7706 5.44
Partly cloudy 40384 28.50
Partly Cloudy 9201 6.49
Patchy heavy snow 31 0.02
Patchy light drizzle 477 0.34
Patchy light rain 249 0.18
Patchy light rain in area with thunder 213 0.15
Patchy light rain with thunder 882 0.62
Patchy light snow 97 0.07
Patchy light snow in area with thunder 5 0.00
Patchy moderate snow 15 0.01
Patchy rain nearby 11870 8.38
Patchy rain possible 48 0.03
Patchy snow nearby 3 0.00
Patchy snow possible 5 0.00
Sunny 40787 28.78
Thundery outbreaks in nearby 283 0.20
Thundery outbreaks possible 29 0.02
Torrential rain shower 17 0.01
TOTAL 141703 100.00
Note: Fuente: GlobalWeatherRepository. Grupo 2.

La variable se organizó bajo un criterio climático práctico, agrupando los distintos estados del tiempo en grupos principales según el clima dominante (como sol, nubes, lluvias o niebla) para que la tabla sea más corta, ordenada y fácil de entende asi nace la tabla simplificada

Tabla simplificada

fila_total_clima <- data.frame(
  Condicion_Climatica_Limpia = "TOTAL",
  ni = sum(tabla_base_clima$ni),
  hi = sum(tabla_base_clima$hi)
)
TDF_condicion_climatica_Final <- rbind(tabla_base_clima, fila_total_clima)
colnames(TDF_condicion_climatica_Final) <- c(
  "Condición climática",
  "Frecuencia Absoluta ni",
  "Frecuencia Relativa hi"
)
kable(TDF_condicion_climatica_Final, align = "c", row.names = FALSE,
      caption = "Tabla N.2: Distribución de frecuencias según el clima dominante de la variable condición climatica en capitales del mundo para el análisis meteorológico del periodo mayo del 2024 a mayo del 2026 ") %>%
  kable_styling(full_width = TRUE, position = "center",
                bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
  row_spec(0, bold = TRUE, color = "white", background = "#2C3E50") %>%
  row_spec(nrow(TDF_condicion_climatica_Final), bold = TRUE, background = "#EAEDED") %>%
  footnote(general = "Fuente: GlobalWeatherRepository. Grupo 2.",
           footnote_as_chunk = TRUE)
Tabla N.2: Distribución de frecuencias según el clima dominante de la variable condición climatica en capitales del mundo para el análisis meteorológico del periodo mayo del 2024 a mayo del 2026
Condición climática Frecuencia Absoluta ni Frecuencia Relativa hi
Nublado/Parcialmente Nublado 59002 41.64
Despejado / Soleado 48388 34.15
Lluvioso / Chubascos 25068 17.69
Neblina / Niebla 7913 5.58
Nieve / Granizo 1010 0.71
Tormenta Electrica 312 0.22
Otros fenomeno local 10 0.01
TOTAL 141703 100.00
Note: Fuente: GlobalWeatherRepository. Grupo 2.

5.- Gráficas

5.1.- Diagrama de barras ni de la tabla simplificada

grafico_barras_ni <- ggplot(tabla_base_clima, aes(x = reorder(Condicion_Climatica_Limpia, -ni), y = ni, fill = Condicion_Climatica_Limpia)) +
  geom_bar(stat = "identity", color = "black", width = 0.6) +
  geom_text(aes(label = scales::comma(ni)), vjust = -0.5, fontface = "bold", size = 3.5) +
  scale_fill_brewer(palette = "Set2") +
  scale_y_continuous(limits = c(0, 70000), breaks = seq(0, 70000, by = 10000), labels = scales::comma) +
  labs(
    title = "Gráfica N.1: Distribución de Frecuencias Absolutas por Condición Climática",
    subtitle = "Periodo: Mayo 2024 - Mayo 2026 | Grupo 2",
    x = "Condición Climática",
    y = "Frecuencia Absoluta (ni)",
    caption = "Fuente: Global Weather Repository"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    plot.title = element_text(face = "bold", size = 13, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 9, face = "bold")
  )

print(grafico_barras_ni)

5.2.- Diagrama de barras hi de la tabla simplificada

grafico_barras_hi <- ggplot(tabla_base_clima, aes(x = reorder(Condicion_Climatica_Limpia, -hi), y = hi, fill = Condicion_Climatica_Limpia)) +
  geom_bar(stat = "identity", color = "black", width = 0.6) +
  geom_text(aes(label = paste0(hi, "%")), vjust = -0.5, fontface = "bold", size = 3.5) +
  scale_fill_brewer(palette = "Set2") +
  scale_y_continuous(limits = c(0, 50), breaks = seq(0, 50, by = 10)) +
  labs(
    title = " Gráfica N.2: Distribución de Frecuencias Relativas por Condición Climática",
    subtitle = "Periodo: Mayo 2024 - Mayo 2026",
    x = "Condición Climática",
    y = "Frecuencia Relativa (hi %)",
    caption = "Fuente: Global Weather Repository"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    plot.title = element_text(face = "bold", size = 13, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 9, face = "bold")
  )

print(grafico_barras_hi)

5.3.- Diagrama de barras con respecto al tamaño muestral

grafico_barras_ni <- ggplot(tabla_base_clima, aes(x = reorder(Condicion_Climatica_Limpia, -ni), y = ni, fill = Condicion_Climatica_Limpia)) +
  geom_bar(stat = "identity", color = "black", width = 0.6) +
  geom_text(aes(label = scales::comma(ni)), vjust = -0.5, fontface = "bold", size = 3.5) +
  scale_fill_brewer(palette = "Set2") +
  scale_y_continuous(limits = c(0, 140000), breaks = seq(0, 140000, by = 20000), labels = scales::comma) +
  labs(
    title = "Gráfica N.3: Distribución de Frecuencias Absolutas por Condición 
    Climática con respeto al tamaño muestral",
    subtitle = "Periodo: Mayo 2024 - Mayo 2026 | Grupo 2",
    x = "Condición Climática",
    y = "Frecuencia Absoluta (ni)",
    caption = "Fuente: Global Weather Repository"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    plot.title = element_text(face = "bold", size = 13, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 9, face = "bold")
  )

print(grafico_barras_ni)

5.4.- Diagrama de barras con respecto al 100%

 grafico_barras_hi <- ggplot(tabla_base_clima, aes(x = reorder(Condicion_Climatica_Limpia, -hi), y = hi, fill = Condicion_Climatica_Limpia)) +
  geom_bar(stat = "identity", color = "black", width = 0.6) +
  geom_text(aes(label = paste0(hi, "%")), vjust = -0.5, fontface = "bold", size = 3.5) +
  scale_fill_brewer(palette = "Set2") +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
  labs(
    title = "Gráfica N.4: Distribución de Frecuencias Relativas por Condición
    Climática",
    subtitle = "Periodo: Mayo 2024 - Mayo 2026",
    x = "Condición Climática",
    y = "Frecuencia Relativa (hi %)",
    caption = "Fuente: Global Weather Repository"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    plot.title = element_text(face = "bold", size = 13, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 9, face = "bold")
  )

print(grafico_barras_hi)

5.5- Diagrama Circular

datos_pie_final <- tabla_base_clima %>%
  arrange(desc(ni)) %>%
  mutate(
    clima_con_porcentaje = paste0(Condicion_Climatica_Limpia, " (", hi, "%)"),
    clima_con_porcentaje = factor(clima_con_porcentaje, levels = clima_con_porcentaje)
  )

grafico_pastel_final <- ggplot(datos_pie_final, aes(x = "", y = hi, fill = clima_con_porcentaje)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar("y", start = 0) + 
  scale_fill_brewer(palette = "Set2") +
  labs(
    title = "                 Gráfica N.5: Proporción de Frecuencias
                       Relativas por Condición Climática para el análisis 
                       meteorológico en capitales del mundo",
    subtitle = "Periodo: Mayo 2024 - Mayo 2026",
    fill = "Condición Climática (hi %)",
    caption = "Fuente: Global Weather Repository"
  ) +
  theme_void() +
  theme(
    plot.title = element_text(face = "bold", size = 13, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, face = "italic"),
    legend.title = element_text(face = "bold", size = 10),
    legend.text = element_text(size = 9),
    legend.position = "right" 
  )
print(grafico_pastel_final)

6.- Indicadores de frecuencia

datos_puros_clima <- tabla_base_clima %>%
  filter(Condicion_Climatica_Limpia != "TOTAL")
frecuencia_max_clima <- max(datos_puros_clima$ni)
Mo_clima <- datos_puros_clima$Condicion_Climatica_Limpia[datos_puros_clima$ni == frecuencia_max_clima]

tabla_indicadores_clima <- data.frame(
  "Variable" = "Condición Climática",
  "Rango" = "-",
  "X" = "-",
  "Me" = "-",
  "Mo" = Mo_clima,
  "V" = "-",
  "Sd" = "-",
  "Cv" = "-",
  "As" = "-",
  "K" = "-",
  "valores Atípicos" = "No presenta valores atípicos",
  stringsAsFactors = FALSE,
  check.names = FALSE 
)
kable(tabla_indicadores_clima, align = "c", row.names = FALSE,
      caption = "Tabla N°3: Indicadores de frecuencia para la variable Condición Climática para el análisis 
                       meteorológico en capitales del mundo") %>%
  kable_styling(full_width = TRUE, position = "center",
                bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
  row_spec(0, bold = TRUE, color = "white", background = "#2C3E50") %>%
  footnote(general = "Fuente: GlobalWeatherRepository.",
           general_title = "Nota: ",
           footnote_as_chunk = TRUE,
           title_format = c("italic", "bold"))
Tabla N°3: Indicadores de frecuencia para la variable Condición Climática para el análisis meteorológico en capitales del mundo
Variable Rango X Me Mo V Sd Cv As K valores Atípicos
Condición Climática
Nublado/Parcialmente Nublado
No presenta valores atípicos
Nota: Fuente: GlobalWeatherRepository.

7.- Conclusiones

En la condición climática que registra una mayor moda fue nublado/parcialmente nublado