I am struggling with data wrangling issue. I want to pivot data with year becoming column names and totals as values, in order to calculate the difference between the months. And then unpivot. I tried tidy.js pivotWider but got a lot of undefined per year.
I know how to approach this with R, but not sure if I am missing something.
initial data
[
{ year: 2020, month: "jan", total: 10 },
{ year: 2020, month: "feb", total: 20 },
{ year: 2019, month: "jan", total: 10 },
{ year: 2019, month: "feb", total: 7 },
{ year: 2018, month: "jan", total: 10 },
{ year: 2018, month: "feb", total: 5 }
]
how I would want it to look for pivoting
[
{ month: "jan", "2020": 10, "2019": 30, "2018": 20, diff: 10 },
{ month: "feb", "2020": 10, "2019": 30, "2018": 20, diff: 10 },
]
Unpivot again
[
{ month: "jan", year: 2020, diff: 10},
{ month: "feb", year: 2020, diff: 10},
{ month: "jan", year: 2019, diff: 10},
{ month: "feb", year: 2019, diff: 10},
]