nanmedian
Computes the median of an array ignoring non-numeric values.
Installation
$ npm install compute-nanmedian
For use in the browser, use browserify.
Usage
var nanmedian = ;
nanmedian( arr[, options] )
Computes the median of an array
ignoring non-numeric values. For unsorted primitive arrays
,
var unsorted = 5 null 3 2 4 null ;var m1 = ;// returns 3.5
The function accepts two options
:
sorted
:boolean
flag indicating if the inputarray
is sorted in ascending order. Default:false
.accessor
: accessorfunction
for accessing values in objectarrays
.
If the input array
is already sorted in ascending order, set the sorted
option to true
.
var sorted = null 2 3 4 null 5 ;var m2 = ;// returns 3.5
For object arrays
, provide an accessor function
for accessing array
values
var data =152null3342546null;{return d 1 ;}var m3 =;// returns 3.5
Note: if provided an array
which does not contain any numeric values, the function returns null
.
Examples
var nanmedian = ;var data = 1001 ;for var i = 0; i < datalength; i++if i % 2 === 0data i = null;elsedata i = Math;console;
To run the example code from the top-level application directory,
$ node ./examples/index.js
Notes
If provided an unsorted input array
, the function is O( N log(N) )
, where N
is the array
length. If the array
is already sorted in ascending order, the function is O(N)
as the function still needs to make a single linear pass to extract numeric values from the array
.
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2015. Philipp Burckhardt.