Skip to content

Instantly share code, notes, and snippets.

@sabha
Created November 28, 2018 17:02
Show Gist options
  • Save sabha/40faa1b9297792fc00e9a50368fee104 to your computer and use it in GitHub Desktop.
Save sabha/40faa1b9297792fc00e9a50368fee104 to your computer and use it in GitHub Desktop.
Stats Frequency
//var data = ['A', 'O', 'A', 'B', 'B', 'AB', 'B', 'B', 'O', 'A', 'O', 'O', 'O', 'AB', 'B', 'AB', 'AB', 'A', 'O', 'A'];
var data = ['A', 'O', 'A','A','A','A', 'A','A','A','A', 'A','A','A','A', 'A','A','A','A', 'A','A','A','A'];
var count = {}
data.forEach((d) => {
count[d] = ((count[d] === undefined) ? 1 : (count[d]+1));
});
var addAll = (accumulator, currentValue) => accumulator + currentValue;
var len = Object.keys(count).map((r) => count[r]).reduce(addAll);
Object.keys(count).map((r) => {
console.log((count[r]/len)*100);
return ({'#': r, '%': (count[r]/len)*100})
});
//95.45454545454545
//4.545454545454546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment