I am currently working with an array structured like this:
["2011-06-16 16:37:20",23.2],
["2011-06-21 16:37:20",35.6],
["2011-06-26 16:37:20",41.8],
["2011-07-01 16:37:20",25],
["2011-07-06 16:37:20",22.8],
["2011-07-11 16:37:20",36.4],
["2011-07-16 16:37:20",34],
["2011-07-21 16:37:20",20]
[...]
Format: [$date,count]
My task now is to enhance each element of the array with a 3rd and 4th value, which are calculated as averages of N counts before or after.
For instance, when N=3
The 3rd value should be the average of the 3 count-values preceding, and the 4th value should be the average of the 3 count-values succeeding the current array element. The resulting array will look like this:
["2011-06-16 16:37:20",23.2, null, 34.13],
["2011-06-21 16:37:20",35.6, null, 29.86],
["2011-06-26 16:37:20",41.8, null, 28.06],
["2011-07-01 16:37:20",25, 33.53, 31.06],
["2011-07-06 16:37:20",22.8, 34.13, 30.13],
["2011-07-11 16:37:20",36.4, 29.86, null],
["2011-07-16 16:37:20",34, 28.06, null],
["2011-07-21 16:37:20",20, 31.06, null]
The null
value is a placeholder for cases where averages cannot be calculated due to insufficient data points. In such cases, the average of all available counts can be displayed instead, like "24.4
" (23.2+35.6)/2 for the 3rd line instead of null
:
["2011-06-26 16:37:20",41.8, 24.4, 28.06],
I am struggling with writing the code to accomplish this task.
I would greatly appreciate any hints or assistance provided. Thank you.
//Update: I apologize for the inconvenience, but could someone please clarify why this question has received 2 downvotes? I am unsure of the reason and would appreciate feedback if I have made a mistake. Sorry for any confusion caused!