0

In Python, it is easy, but in JavaScript, it is giving me such a headache.

I am using Chart.js, D3 and Date-FNS. I am using CSV. You can check my CSV file here.

I would like to get the difference of cases or deaths between each day, so theoretically I could get the number of new cases or deaths per day instead of total cases. I mean to resample every 2 days using a arithmetic operator. It's just subtracting that is giving me such a headache.

Similar to Python's Pandas' diff() in the questions:

I was suggested to use map() and reduce(), but nothing worked.

Inspired by Subtract values in column in JS, I tried:

  const DailyDeathsData = dates.map((d, pair) =>
  {
      var firstVal = parseInt(d.totalDeaths[pair]);
      var secondVal = parseInt(d.totalDeaths[pair + 1]);
      var daily = (firstVal > secondVal) ? firstVal - secondVal : secondVal - firstVal;
      return daily;
  });

But it returned all zeros.

Then, inspired by JavaScript go through array and subtract each item with next, using slice, map and reduce:

const DailyDeathsData = dates.slice(1).map((v, i) => v.totalDeaths - dates[i].totalDeaths);

It also returned all zeros.

Then, using reduce:

const DailyDeathsData = dates.reduce((v1, v2) => { return v1.totalDeaths - v2.totalDeaths });

But it did not display.

Oo'-
  • 203
  • 6
  • 22
  • Just a side note: if `d.totalDeaths[pair]` represents the number of deaths from the beginning of the records to day X and `d.totalDeaths[pair + 1]` represents the number of deaths from the beginning of the records to day X+1, the check `(firstVal > secondVal) ? ...` is pointless as `secondVal` will always be greater than or equal to `firstVal` (unless people begin coming back from death as zombies XP). – secan Jun 03 '21 at 15:21
  • I used this condition because of negative subtraction, for example, 21 deaths - 23 deaths = -2 days. I have just removed this condition and tested. It returned all zeroes. – Oo'- Jun 03 '21 at 15:25
  • Is `d.totalDeaths[pair]` the *total* number of deaths from day 1 to day X? If it is, you know for a fact that the *total* number of deaths from day 1 to day X **cannot** be greater than the number of deaths from day 1 to day X+1. The *total* number of deaths from day 1 can only increase or stay stable as time passes; it cannot decrease. What can decrease is the *daily* number of deaths. – secan Jun 03 '21 at 15:34
  • Is it something like this, you are trying to achieve? https://jsfiddle.net/hk5ts7nz/ – secan Jun 03 '21 at 15:54
  • I compare the graphs results in JavaScript and Python. Exactly, @secan. Put your answer that I conclude. I also used `d[pair].totalDeaths` too but I understood now, that it had to be `dates` instead of `d`, now it makes sense. – Oo'- Jun 03 '21 at 16:03

1 Answers1

1

If I understood correctly what you want to achieve, you can do something similar to:

const data = [{
    city: 'Aparecida',
    date: '2021-01-04',
    totalDeaths: 21,
    totalCases: 781,
    totalRecovered: 724,
    totalBeds: 13,
    intubated: 1
  },
  {
    city: 'Aparecida',
    date: '2021-01-05',
    totalDeaths: 22,
    totalCases: 822,
    totalRecovered: 724,
    totalBeds: 8,
    intubated: 3
  },
  {
    city: 'Aparecida',
    date: '2021-01-06',
    totalDeaths: 22,
    totalCases: 831,
    totalRecovered: 724,
    totalBeds: 11,
    intubated: 2
  },
  {
    city: 'Aparecida',
    date: '2021-01-07',
    totalDeaths: 22,
    totalCases: 847,
    totalRecovered: 724,
    totalBeds: 15,
    intubated: 2
  },
  {
    city: 'Aparecida',
    date: '2021-01-10',
    totalDeaths: 22,
    totalCases: 857,
    totalRecovered: 724,
    totalBeds: 16,
    intubated: 4
  }
];

const totalDeathsCurve = data.map((item, index) => {
  if (index === 0) return 0;
  return data[index].totalDeaths - data[index - 1].totalDeaths
});

console.log(totalDeathsCurve);

In a nutshell, you map your data and:

  1. set the totalDeaths of the first item as your starting point (0)
  2. subtract from the current item's totalDeaths the previous one to obtain the daily total

Anyway, please be aware that if you have missing days in your data (and you have; for exemple you go from 2021-01-07 directly to 2021-01-10) the difference will not represent an actual daily difference; it will merely represent the difference with the previous entry

secan
  • 2,622
  • 1
  • 7
  • 24