# How to use the simple-statistics.sum function in simple-statistics

## To help you get started, weâ€™ve selected a few simple-statistics examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

antvis / data-set / src / transform / percent.ts View on Github
``````forIn(groups, (group) => {
const totalSum = sum(group.map((row: any) => row[field]));
if (totalSum === 0) {
console.warn(`Invalid data: total sum of field \${field} is 0!`);
}
const innerGroups = partition(group, [dimension]);
forIn(innerGroups, (innerGroup) => {
const innerSum = sum(innerGroup.map((row: any) => row[field]));
// const resultRow = pick(innerGroup[0], union(groupBy, [ dimension ]));
const resultRow = innerGroup[0];
// FIXME in case dimension and field is the same
const dimensionValue = resultRow[dimension];
resultRow[field] = innerSum;
resultRow[dimension] = dimensionValue;
if (totalSum === 0) {
resultRow[as!] = 0;
} else {
resultRow[as!] = innerSum / totalSum;``````
antvis / data-set / src / transform / percent.ts View on Github
``````forIn(innerGroups, (innerGroup) => {
const innerSum = sum(innerGroup.map((row: any) => row[field]));
// const resultRow = pick(innerGroup[0], union(groupBy, [ dimension ]));
const resultRow = innerGroup[0];
// FIXME in case dimension and field is the same
const dimensionValue = resultRow[dimension];
resultRow[field] = innerSum;
resultRow[dimension] = dimensionValue;
if (totalSum === 0) {
resultRow[as!] = 0;
} else {
resultRow[as!] = innerSum / totalSum;
}
result.push(resultRow);
});
});``````
apinf / platform / feedback / collection / helpers.js View on Github
``````sumOfVotes () {
// Get all votes for current feedback
const allFeedback = FeedbackVotes.find({ feedbackId: this._id }).fetch();

// Create a list of all feedback vote values
const votes = allFeedback.map((feedback) => {
return feedback.vote;
});

// Calculate the sum of all vote values
return sum;
},
author () {``````
apinf / platform / apinf_packages / feedback / collection / helpers.js View on Github
``````sumOfVotes () {
// Get all votes for current feedback
const allFeedback = FeedbackVotes.find({ feedbackId: this._id }).fetch();

// Create a list of all feedback vote values
const votes = allFeedback.map((feedback) => {
return feedback.vote;
});

// Calculate the sum of all vote values
return sum;
},
author () {``````
electron / i18n / script / wordcount.ts View on Github
``````function analyze(dir: string, title: string) {
const files = walk(dir, { directories: false }).map(f =&gt; path.join(dir, f))
const words = chain(files)
.flatten()
.value()
const counts = files.map(
f =&gt; (matchWords(fs.readFileSync(f, 'utf8')) || []).length
)
const { average, sum } = require('simple-statistics')

const results: Record = {
'total files': files.length,
'total words': sum(counts),
'unique words': chain(words)
.flatten()
.uniq()
.value().length,
'average words per file': Math.floor(average(counts)),
}

console.log(`\n## \${title}\n`)
console.log('Stat | Value')
console.log('---- | -----')

Object.keys(results).forEach(stat =&gt; {
console.log([stat, results[stat]].join(' | '))
})
}``````
Turfjs / turf / lib / sum.js View on Github
``````_.each(polyFC.features, function(poly){
if(!poly.properties){
poly.properties = {}
}
var values = []
_.each(ptFC.features, function(pt){
if (t.inside(pt, poly)) {
values.push(pt.properties[inField])
}
})
poly.properties[outField] = ss.sum(values)
})``````
antvis / data-set / src / transform / waffle.ts View on Github
``````forIn(groups, (group) => {
const totalValue = sum(map(group, (row: any) => row[valueField]));
let cols = Math.ceil((totalValue * scale) / rows);
if (totalValue * scale > maxCount) {
scale = maxCount / totalValue;
cols = Math.ceil((totalValue * scale) / rows);
}
wStep = width / cols;
});``````
antvis / data-set / src / transform / kernel-smooth / regression.ts View on Github
``````kernelSmoother = vectorize((x: number[]) => {
const weights = xs.map((x_i) => weightFunc(x, x_i));
const num = sum(weights.map((w, i) => w * ys[i]));
const denom = sum(weights);
if (!num || !denom) return 0;
return num / denom;
});
}``````
antvis / data-set / src / transform / kernel-smooth / regression.ts View on Github
``````kernelSmoother = vectorize((x) => {
const weights = xs.map((x_i) => weightFunc(x, x_i));
const num = sum(weights);
const denom = xCount * bandwidth;
if (!num || !denom) return 0;
return num / denom;
});
} else {``````

## simple-statistics

Simple Statistics

ISC