A cross tabulation library for Node.js
A cross tabulation library simulating R's API.
$ npm install xtabs
var xtabs = require"xtabs";var data =// null represents absence of data, like NA in R.department: "RD" "RD" "HR" "GA" null "GA" "RD"gender: "M" "F" null "M" "F" "M" "M";// Make a cross tabulation by department and gender.var t = xtabstabledata "department" "gender";console.logtget"HR" "M"; // 0console.logtget"RD" "F"; // 1console.logtdim; // [3, 2]console.logtdimnames;// Use to indicate that you want the whole data.// t.get(undefined, "M") is equivalent to t[, "M"] in R.console.logtget "M"get"GA"; // 2// You can also use number indices to retrieve data.// t.get(0) is equivalent to t.get(0, undefined).console.logtget0dim; // // You can use list indices as well.console.logtget"RD" "GA" "F"dim; // // List indices can also be in number form.console.logtget0 2 1dim; // 
varreturn areduce return x + y ;;varreturn areduce return x * y ;;var t = xtabstabledata "department" "gender";// This adds a row 'Sum'.var t_ = xtabsaddMarginst 0 n: "Sum" f: sum ;console.logt_get"Sum" "M"; // 4console.logt_get"Sum"array; // [4, 1]// This adds a column 'Sum'.var t_ = xtabsaddMarginst 1 n: "Sum" f: sum ;console.logt_get"RD" "Sum"; // 3console.logt_get "Sum"array; // [3, 0, 2]// This adds both a row 'Sum' and a column 'Sum'.var t_ = xtabsaddMarginst 0 1 n: "Sum" f: sum ;console.logt_get"Sum" "Sum"; // 5// This adds 'Sum' and 'Prod' rows and a 'Sum' column.var t_ = xtabsaddMarginst 0 1n: "Sum" f: sum n: "Prod" f: prod n: "Sum" f: sum ;
var t = xtabstabledata "department" "gender";// Proportions calculated in relation to all data.var t_ = xtabspropt;console.logt_get"RD"array; // [0.4, 0.2]// Proportions calculated in relation to rows.var t_ = xtabspropt 0;console.logt_get"RD"array; // [2/3, 1/3]// Proportions calculated in relation to columns.var t_ = xtabspropt 1;console.logt_get "M"array; // [0.5, 0, 0.5]
levels property. You may refer to R's factor to better understand it. If
useNull is true, then
null is counted as level too, this simulates R's factor not excluding NA's behavior.
o is a factor.
Converts back a factor to its string representation.
Creates a cross tabulation.
x is an array or factor, it is being used as the data source for the tabulation.
x is an object with array/factor properties, then subsequent
variables are provided as tabulation variables. For example:
xtabstableemployees "department" "gender"
The above means that employees is the data source (a table in fact) containing department and gender data, and you want to make a cross tabulation by department and then by gender. The result in this particular case is a two-dimensional table, sort of like this:
genderdepartment M FRD 2 1HR 0 0GA 2 0
For more about behaviors about cross tabulation, you can refer to R's
xtabs function (Hence the name of this project).
useNull is true, then
null is counted too.
Add margins to a table. For a two-dimensional table, that means adding rows and columns, margin 0 represents rows, margin 1 represents columns.
fun is the function to apply. E.g:
Given the following table:genderdepartment M FRD 2 1HR 0 0GA 2 0After adding a margin 0 using a sum function, you will get:genderdepartment M FRD 2 1HR 0 0GA 2 0Sum 4 1If you add a margin 1 instead, you will get:genderdepartment M F SumRD 2 1 3HR 0 0 0GA 2 0 2
You can add multiple margins at once, or apply multiple functions at once, or both. If you want to add multiple margins at once, you pass in an array of margins to be added in order, instead of a single margin. If you want to apply multiple functions, you can provide a list of function objects (See Adding margins) per margin. Functions are applied in the same order the margins are provided.
Return a new table with proportions according to
margin is undefined or null, then proportions are calculated in relation of the whole table. See Proportions of a table for examples of how to use it. Also refer to the following diagrams:
Original table:genderdepartment M FRD 2 1HR 0 0GA 2 0Proportions in relation to rows:genderdepartment M FRD 2/3 1/3HR NaN NaNGA 2/2 0/2Proportions in relation to columns:genderdepartment M FRD 2/4 1/1HR 0/4 0/1GA 2/4 0/1Proportions in relation to whole:genderdepartment M FRD 2/5 1/5HR 0/5 0/5GA 2/5 0/5
This is what you get when you call xtabs.table, however, this class is not directly exposed to you.
The dimensions of the table. It's an array with each dimension's length.
An array containing each dimension's names, each one has a
dim property indicating the dimension's name and a
names property indicating each level's name in the dimension.
The array representing the table. This is underline data structure of the table.
Gets data out of the table. Basically, you pass in each variable's index or indices to get the data out. You can also pass in
as a special way to indicate that you want all the data of this variable. You can refer to the Examples section for the various ways to use this method.
(The MIT License)
Copyright (c) 2013 Seth Yuan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.