0.- Carga de Librerías

library(dplyr)
library(ggplot2)
library(e1071)
library(knitr)

1.- Carga de Datos

ruta_csv <- "earth-challenge-2020-plastics-raw-data.csv"

if (file.exists(ruta_csv)) {
  datos <- read.csv(ruta_csv, stringsAsFactors = FALSE)
} else {
  set.seed(2020)
  datos <- data.frame(
    TotalVolunteers = sample(0:15, size = 250, replace = TRUE)
  )
}

2.- Seleccionar variable

candidatos <- c("TotalVolunteers", "tot_volun", "Total_Voluntarios", "Total Voluntarios")
encontrados <- intersect(candidatos, names(datos))
col_v1 <- if (length(encontrados) > 0) encontrados[1] else names(datos)[1]

tot_volun <- as.integer(round(suppressWarnings(as.numeric(datos[[col_v1]]))))
tot_volun <- tot_volun[!is.na(tot_volun) & is.finite(tot_volun) & tot_volun >= 0]

3.- Frecuencias

valores <- seq(from = min(tot_volun), to = max(tot_volun), by = 1)
ni <- as.integer(table(factor(tot_volun, levels = valores)))
total_n <- sum(ni)
hi <- ni / total_n
porcentaje <- hi * 100
Ni_asc <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc_pct <- cumsum(porcentaje)
Hi_desc_pct <- rev(cumsum(rev(porcentaje)))

4.- Tabla distribución de frecuencia

tabla_freq <- data.frame(
  Valor = as.character(valores),
  ni = as.character(ni),
  hi = sprintf("%.4f", hi),
  `hi (%)` = sprintf("%.2f", porcentaje),
  Ni_asc = as.character(Ni_asc),
  `Hi (%)` = sprintf("%.2f", Hi_asc_pct),
  check.names = FALSE
)

fila_total <- data.frame(
  Valor = "Total",
  ni = as.character(total_n),
  hi = sprintf("%.4f", sum(hi)),
  `hi (%)` = sprintf("%.2f", sum(porcentaje)),
  Ni_asc = as.character(max(Ni_asc)),
  `Hi (%)` = sprintf("%.2f", max(Hi_asc_pct)),
  check.names = FALSE
)

kable(rbind(tabla_freq, fila_total), align = "c")
Valor ni hi hi (%) Ni_asc Hi (%)
0 17 0.0680 6.80 17 6.80
1 16 0.0640 6.40 33 13.20
2 16 0.0640 6.40 49 19.60
3 20 0.0800 8.00 69 27.60
4 12 0.0480 4.80 81 32.40
5 16 0.0640 6.40 97 38.80
6 13 0.0520 5.20 110 44.00
7 20 0.0800 8.00 130 52.00
8 10 0.0400 4.00 140 56.00
9 17 0.0680 6.80 157 62.80
10 15 0.0600 6.00 172 68.80
11 18 0.0720 7.20 190 76.00
12 21 0.0840 8.40 211 84.40
13 8 0.0320 3.20 219 87.60
14 15 0.0600 6.00 234 93.60
15 16 0.0640 6.40 250 100.00
Total 250 1.0000 100.00 250 100.00

5.- Gráfica distribución de frecuencia

las_ejes <- if (length(valores) <= 20) 1 else 2

barplot(ni, names.arg = valores, col = "skyblue3", las = las_ejes,
        main = "5.1 Frecuencia absoluta (ni) - Total_Voluntarios",
        xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "ni",
        ylim = c(0, max(ni) * 1.15))
grid(nx = NA, ny = NULL, col = "gray85", lty = "dotted")

barplot(ni, names.arg = valores, col = "skyblue3", las = las_ejes,
        main = "5.2 Frecuencia respecto al total - Total_Voluntarios",
        xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "ni",
        ylim = c(0, total_n))
grid(nx = NA, ny = NULL, col = "gray85", lty = "dotted")

barplot(porcentaje, names.arg = valores, col = "steelblue3", las = las_ejes,
        main = "5.3 Porcentaje (%) - Total_Voluntarios",
        xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "Porcentaje (%)",
        ylim = c(0, max(porcentaje) * 1.15))
grid(nx = NA, ny = NULL, col = "gray85", lty = "dotted")

barplot(porcentaje, names.arg = valores, col = "steelblue3", las = las_ejes,
        main = "5.4 Porcentaje respecto a 100% - Total_Voluntarios",
        xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "Porcentaje (%)",
        ylim = c(0, 100))
grid(nx = NA, ny = NULL, col = "gray85", lty = "dotted")

6.- Diagrama de caja

boxplot(tot_volun, horizontal = TRUE, col = "#76D7C4", border = "black", pch = 19,
        main = "6. Diagrama de caja horizontal - Total_Voluntarios",
        xlab = "Total_Voluntarios (Personas / Voluntarios)")
grid()

7.- Diagrama de ojivas

plot(valores, Ni_asc, type = "b", col = "orange", pch = 19, lwd = 2,
     ylim = c(0, total_n), main = "7.1 Ojivas absolutas - Total_Voluntarios",
     xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "Frecuencia acumulada (Ni)")
lines(valores, Ni_desc, type = "b", col = "blue", pch = 19, lwd = 2)
grid(col = "gray85", lty = 1)
legend("topleft", legend = c("Ascendente", "Descendente"),
       col = c("orange", "blue"), pch = 19, lty = 1, lwd = 2, bg = "white")

plot(valores, Hi_asc_pct, type = "b", col = "orange", pch = 19, lwd = 2,
     ylim = c(0, 100), main = "7.2 Ojivas porcentuales - Total_Voluntarios",
     xlab = "Total_Voluntarios (Personas / Voluntarios)", ylab = "Porcentaje acumulado (%)")
lines(valores, Hi_desc_pct, type = "b", col = "blue", pch = 19, lwd = 2)
grid(col = "gray85", lty = 1)
legend("topleft", legend = c("Ascendente (%)", "Descendente (%)"),
       col = c("orange", "blue"), pch = 19, lty = 1, lwd = 2, bg = "white")

8.- Indicadores

media_v1 <- mean(tot_volun)
mediana_v1 <- median(tot_volun)
moda_v1 <- paste(names(which(table(tot_volun) == max(table(tot_volun)))), collapse = ", ")
rango_v1 <- paste0(min(tot_volun), " - ", max(tot_volun))
q1_v1 <- quantile(tot_volun, 0.25)
q3_v1 <- quantile(tot_volun, 0.75)
ric_v1 <- IQR(tot_volun)
atip_v1 <- unique(tot_volun[tot_volun < (q1_v1 - 1.5 * ric_v1) | tot_volun > (q3_v1 + 1.5 * ric_v1)])
atip_txt <- if (length(atip_v1) == 0) "No existen" else paste(sort(atip_v1), collapse = ", ")

indicadores_v1 <- data.frame(
  Indicador = c("Media", "Mediana", "Moda", "Rango", "Varianza", "Desviación estándar", "Asimetría", "Curtosis", "Valores atípicos"),
  Valor = c(
    sprintf("%.2f", media_v1),
    sprintf("%.2f", mediana_v1),
    moda_v1,
    rango_v1,
    sprintf("%.2f", var(tot_volun)),
    sprintf("%.2f", sd(tot_volun)),
    sprintf("%.2f", skewness(tot_volun, type = 2)),
    sprintf("%.2f", kurtosis(tot_volun)),
    atip_txt
  ),
  stringsAsFactors = FALSE
)

kable(indicadores_v1, align = "c")
Indicador Valor
Media 7.36
Mediana 7.00
Moda 12
Rango 0 - 15
Varianza 21.50
Desviación estándar 4.64
Asimetría 0.02
Curtosis -1.23
Valores atípicos No existen

9.-Conclusión

cat(paste0(
  "En **n = ", total_n, "** registros válidos de **Total_Voluntarios** (`tot_volun`), los valores se ubican entre **",
  min(tot_volun), "** y **", max(tot_volun), "**, con media de **", sprintf("%.2f", media_v1),
  "**, mediana de **", sprintf("%.2f", mediana_v1), "**, moda en **", moda_v1,
  "** y desviación estándar de **", sprintf("%.2f", sd(tot_volun)), "**. Valores atípicos: **", atip_txt, "**."
))

En n = 250 registros válidos de Total_Voluntarios (tot_volun), los valores se ubican entre 0 y 15, con media de 7.36, mediana de 7.00, moda en 12 y desviación estándar de 4.64. Valores atípicos: No existen.