npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stdlib/stats-ranks

v0.2.2

Published

Compute ranks for values of an array-like object.

Downloads

1,497

Readme

ranks

NPM version Build Status Coverage Status

Compute ranks for values of an array-like object.

Installation

npm install @stdlib/stats-ranks

Usage

var ranks = require( '@stdlib/stats-ranks' );

ranks( arr[, opts] )

Returns the sample ranks of the elements in arr, which can be either an array or typed array.

var arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ];
var out = ranks( arr );
// returns [ 2, 3, 5, 1, 4 ]

// Ties are averaged:
arr = [ 2, 2, 1, 4, 3 ];
out = ranks( arr );
// returns [ 2.5, 2.5, 1, 5, 4 ];

// Missing values are placed last:
arr = [ null, 2, 2, 1, 4, 3, NaN, NaN ];
out = ranks( arr );
// returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ]

The function accepts the following options:

  • method: string indicating how ties are handled. Can be one of the following values: 'average', 'min', 'max', 'ordinal' and 'dense'. Default: 'average'.
  • missing: string specifying how missing values are handled. Must be either 'last', 'first' or 'remove'. Default: 'last'.
  • encoding: array holding all values which will be regarded as missing values. Default: [ NaN, null].

When all elements of the array are different, the ranks are uniquely determined. When there are equal elements (called ties), the method option determines how they are handled. The default, 'average', replace the ranks of the ties by their mean. Other possible options are 'min' and 'max', which replace the ranks of the ties by their minimum and maximum, respectively. 'dense' works like 'min', with the difference that the next highest element after a tie is assigned the next smallest integer. Finally, ordinal gives each element in arr a distinct rank, according to the position they appear in.

var data = [ 2, 2, 1, 4, 3 ];

// Max method:
var out = ranks( data, {
    'method': 'max'
});
// returns [ 3, 3, 1, 5, 4 ]

// Min method:
out = ranks( data, {
    'method': 'min'
});
// returns [ 2, 2, 1, 5, 4 ]

// Ordinal method
out = ranks( data, {
    'method': 'ordinal'
});
// returns [ 2, 3, 1, 5, 4 ]

// Dense method:
out = [ 2, 2, 1, 4, 3 ];
out = ranks( data, {
    'method': 'dense'
});
// returns [ 2, 2, 1, 4, 3 ]

The missing option is used to specify how to handle missing data. By default, NaN or null are treated as missing values. 'last'specifies that missing values are placed last, 'first' that the are assigned the lowest ranks and 'remove' means that they are removed from the array before the ranks are calculated.

var data = [ NaN, 2, 2, 1, 4, 3, null, null ];

var out = ranks( data, {
    'missing': 'first'
});
// returns [ 1, 5.5, 5.5, 4, 8, 7, 2, 3 ]

out = ranks( data, {
    'missing': 'last'
});
// returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ]

out = ranks( data, {
    'missing': 'remove'
});
// returns [ 2.5, 2.5, 1, 5, 4 ]

Custom encoding for missing values is supported via the encoding option, which allows to supply the function with an array of values which should be treated as missing.

var Int32Array = require( '@stdlib/array-int32' );

var data = new Int32Array( [ 2, 1, -999, 3, 4 ] );

var out = ranks( data, {
    'encoding': [ -999 ]
});
// returns [ 2, 1, 5, 3, 4 ]

Examples

var Int32Array = require( '@stdlib/array-int32' );
var round = require( '@stdlib/math-base-special-round' );
var randu = require( '@stdlib/random-base-randu' );
var ranks = require( '@stdlib/stats-ranks' );

var data;
var out;
var i;

// Plain arrays...
data = new Array( 10 );
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = round( randu()*10.0 );
}

out = ranks( data );
// returns <array>

// Typed arrays...
data = new Int32Array( 10 );
for ( i = 0; i < data.length; i++ ) {
    data[ i ] = randu() * 10.0;
}

out = ranks( data );
// returns <array>

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.