array-statistics Documentation

array-statistics is a simple to use package for performing statistical calculations on arrays of numbers in JavaScript.

Install

npm i array-statistics

Package Links

Note: All of the exported functions in this package will attempt to convert any strings in your array to numbers (e.g., "5.67" to 5.67, "100,000.56" to 100000.56, "$56.58" to 56.58). All other values will be filtered out of the calculations. See below for more details.

Basic Example

let { sum, count, mean, median, mode, minimum, maximum, range, standardDeviation, } = require("array-statistics") let array = [10, 20, 30, 10, 20] console.log('sum:', sum(array)) // sum: 90 console.log('count:', count(array)) // count: 5 console.log('mean:', mean(array)) // mean: 18 console.log('median:', median(array)) // median: 20 console.log('mode:', mode(array)) // mode: [10, 20] console.log('minimum:', minimum(array)) // minimum: 10 console.log('maximum:', maximum(array)) // maximum: 30 console.log('range:', range(array)) // range: 20 console.log('standardDeviation:', standardDeviation(array)) // standardDeviation: 7.483314773547883

Conversions, Value Filtering, and Error Handling

  • Arrays without any valid numbers will throw an error.
  • Booleans, undefined, null, blank strings, and other strings that can't be converted to numbers will be removed from the array before running any of the exported functions.
  • All exported functions will attempt to convert strings to numbers for every value in your array. In doing so, they'll remove all characters except 0-9, . (decimal point), and - (minus).
  • Note that commas are removed from string values, then converted to numbers. A string intending to use a "decimal comma", would end up converting "123,45" to 12345
let mixedArray = [ 0, 9, 987, "1234.56", "123,456,789", "$58.23", "€200,000.80", true, false, "120,00", "Pizza", undefined, null, ] // becomes => // [ 0, 9, 987, 1234.56, 123456789, 58.23, 200000.8, 12000 ]

array-statistics API

All of array-statistics' exported functions take an array parameter.

Though every function will attempt to convert strings in your array to numbers and remove those that can't be converted, we recommend only passing arrays containing numbers to ensure everything behaves the way you would expect it to.

sum(array)

Calculates the sum of all of an array's numbers (including strings converted to numbers by the function). Returns a number.

count(array)

After converting any strings that can be converted to numbers, counts all valid numbers (the array's length). Returns a number.

mean(array)

Calculates the arithmetic mean of the array's values (including strings converted to numbers by the function). Returns a number.

median(array)

Finds the median of the of the array's number set (including strings converted to numbers by the function). Returns a number.

mode(array)

Finds the mode or modes of the of the array's number set (including strings converted to numbers by the function). Returns an array.

minimum(array)

Finds the smallest number in an array (including strings converted to numbers by the function). Returns a number.

maximum(array)

Finds the largest number in an array (including strings converted to numbers by the function). Returns a number.

range(array)

Calculates the range of the passed array's values (including strings converted to numbers by the function). Returns a number.

sumOfSquares(array)

Given an array of numbers (including strings converted to numbers by the function), sumOfSquares calculates the sum of squares. Returns a number.

sampleVariance(array)

Given an array of numbers (including strings converted to numbers by the function) for a sample data set, sampleVariance calculates the sample variance. Returns a number.

populationVariance(array)

Given an array of numbers (including strings converted to numbers by the function) for a population data set, populationVariance calculates the population variance. Returns a number.

sampleStandardDeviation(array)

Given an array of numbers (including strings converted to numbers by the function) for a sample data set, sampleStandardDeviation calculates the sample standard deviation. Returns a number.

populationStandardDeviation(array)

Given an array of numbers (including strings converted to numbers by the function) for a population data set, populationStandardDeviation calculates the population standard deviation. Returns a number.

big.js Dependency

At the time of this packages launch, its sole dependency is big.js. Whenever possible, big.js is used for all mathematical operations to improve decimal precision.

LICENSE

License information for the array-statistics package can be found on GitHub.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.