function calcularPromedioGHG1(data) {
const promedioPorPais = {};
data.forEach(entry => {
if (!promedioPorPais[entry.Country]) {
promedioPorPais[entry.Country] = {
total_08_12: 0,
count_08_12: 0,
total_13_20: 0,
count_13_20: 0,
ghg1990: 0,
};
}
if (entry.year === 1990) {
promedioPorPais[entry.Country].ghg1990 = entry.ghg;
} else if (entry.year >= 2008 && entry.year <= 2012) {
promedioPorPais[entry.Country].total_08_12 += entry.ghg;
promedioPorPais[entry.Country].count_08_12 += 1;
} else if (entry.year >= 2013 && entry.year <= 2020) {
promedioPorPais[entry.Country].total_13_20 += entry.ghg;
promedioPorPais[entry.Country].count_13_20 += 1;
}
});
const resultados = Object.keys(promedioPorPais).map(pais => {
const { total_08_12, count_08_12, total_13_20, count_13_20, ghg1990 } = promedioPorPais[pais];
const ghgPromedio_08_12 = count_08_12 > 0 ? total_08_12 / count_08_12 : 0;
const ghgPromedio_13_20 = count_13_20 > 0 ? total_13_20 / count_13_20 : 0;
return {
Country: pais,
ghg1990: ghg1990,
ghgPromedio_08_12: ghgPromedio_08_12.toFixed(2),
ghgPromedio_13_20: ghgPromedio_13_20.toFixed(2),
};
});
return resultados;
}