README.md (3303B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # tabulate 22 23 > Generate a frequency table. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var tabulate = require( '@stdlib/utils/tabulate' ); 41 ``` 42 43 #### tabulate( collection ) 44 45 Generates a frequency table. 46 47 ```javascript 48 var arr = [ 'beep', 'boop', 'foo', 'beep' ]; 49 50 var out = tabulate( arr ); 51 // returns [ [ 'beep', 2, 0.5 ], [ 'boop', 1, 0.25 ], [ 'foo', 1, 0.25 ] ] 52 ``` 53 54 The returned frequency table is an `array` of `arrays`. Each sub-array corresponds to a unique value in the input `collection` and is structured as follows: 55 56 - `0`: unique value 57 - `1`: value count 58 - `2`: frequency percentage 59 60 </section> 61 62 <!-- /.usage --> 63 64 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 65 66 <section class="notes"> 67 68 ## Notes 69 70 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 71 72 </section> 73 74 <!-- /.notes --> 75 76 <!-- Package usage examples. --> 77 78 <section class="examples"> 79 80 ## Examples 81 82 <!-- eslint no-undef: "error" --> 83 84 ```javascript 85 var randu = require( '@stdlib/random/base/randu' ); 86 var floor = require( '@stdlib/math/base/special/floor' ); 87 var tabulate = require( '@stdlib/utils/tabulate' ); 88 89 var vals; 90 var arr; 91 var out; 92 var i; 93 var j; 94 95 vals = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ]; 96 97 // Generate a random collection... 98 arr = new Array( 100 ); 99 for ( i = 0; i < arr.length; i++ ) { 100 j = floor( randu()*vals.length ); 101 arr[ i ] = vals[ j ]; 102 } 103 104 // Generate a frequency table: 105 out = tabulate( arr ); 106 console.log( out ); 107 ``` 108 109 </section> 110 111 <!-- /.examples --> 112 113 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 114 115 <section class="references"> 116 117 </section> 118 119 <!-- /.references --> 120 121 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 122 123 <section class="links"> 124 125 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 126 127 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 128 129 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 130 131 </section> 132 133 <!-- /.links -->