Last active
April 28, 2020 17:36
-
-
Save stekhn/e8b7f5a2b8bcc44d80ec20d68efb0e71 to your computer and use it in GitHub Desktop.
Calculate the the daily reproduction numbers (R) from an object array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reproNumber(data, steps = 4, key = 'value') { | |
return data.map((obj, i) => { | |
const currentDays = data.slice(i - steps, i); | |
const previousDays = data.slice(i - steps * 2, i - steps); | |
const currentDaysSum = currentDays.reduce((sum, curr) => { return sum + curr[key]; }, 0); | |
const previousDaysSum = previousDays.reduce((sum, curr) => { return sum + curr[key]; }, 0); | |
return Object.assign({}, obj, { | |
r: currentDaysSum / previousDaysSum | |
}); | |
}).filter(d => d.r && isFinite(d.r)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment