SkillAgentSearch skills...

Ply.js

ply.js is a small javascript library designed to to group, summarize, and manipulate tabular data sets, using an API inspired by Hadley Wickham's dplyr library for R.

Install / Use

/learn @hamilton/Ply.js
About this skill

Quality Score

0/100

Category

Design

Supported Platforms

Universal

README

ply.js

Build Status

ply.js is a small javascript library designed to to group, summarize, and manipulate tabular data sets, using an API inspired by Hadley Wickham's dplyr library for R. It's meant to work in browsers with the kind of tabular data you might encounter on the web - arrays of objects.

Here's an example:

let data = [
    {u: true,  v: true,  w: 'h', x: 'a', y: 1, z: 100},
    {u: false, v: true,  w: 'h', x: 'a', y: 1, z: 100},
    {u: true,  v: true,  w: 'i', x: 'a', y: 1, z: 100},
    {u: false, v: true,  w: 'i', x: 'a', y: 1, z: 100},
	...
]

let sd = new Ply(data)
  .group('w', 'x')  // groups the data by variables w and x
  .reduce({         // describes what new variables to create with our grouping
    ySum: (arr) => arr.reduce((acc,v)=>acc+v.y,0),
    yLength: (arr) => arr.length                        
  })
  .transform()      // returns the results

group a small data set and sum / count the values.

let data = [
    {date: '2010-01-02', color: 'green', x: 100},
    {date: '2010-01-02', color: 'green', x: 120},
    {date: '2010-01-03', color: 'green', x: 142},
    {date: '2010-01-03', color: 'green', x: 130},
    {date: '2010-01-02', color: 'red', x: 70},
    {date: '2010-01-02', color: 'red', x: 87},
    {date: '2010-01-03', color: 'red', x: 95},
    {date: '2010-01-03', color: 'red', x: 99}
]

// 1. group by color and date
// 2. get length of groups, as well as sum of x per grouping
let byColorAndDate = new Ply(data)
    .group('date', 'color') 
    .reduce({
      xSum:    arr => arr.reduce((acc,v)=>acc+v.x, 0),
      xLength: arr => arr.length
     })
    .transform()

get mean, median, and standard deviation of groups

Here, we use some reducer helper functions to get summary statistics of our group.

let data = [
    {date: '2018-01-02', color: 'green', x: 100},
    {date: '2018-01-02', color: 'green', x: 120},
    {date: '2018-01-03', color: 'green', x: 142},
    {date: '2018-01-03', color: 'green', x: 130},
    {date: '2018-01-02', color: 'red', x: 70},
    {date: '2018-01-02', color: 'red', x: 87},
    {date: '2018-01-03', color: 'red', x: 95},
    {date: '2018-01-03', color: 'red', x: 99},
    ... // let's say 1,000 more rows
]

let summary = new Ply(data)
  .group('date', 'color')
  .reduce({
    mean: Ply.mean('x'),
    median: Ply.median('x'),
    stdev: Ply.standardDeviation('x'),
    IQR: Ply.IQR('x'),
    q75: Ply.quantile(.75, 'x')
  })
  .transform()

API

basic features

  • new Ply(data) Creates a new Ply object with data.
  • .group(...facets) Creates a new grouped data set based on the facet keys. The resulting object has keys based on the facet combinations, and values which are the row elements that contain those facets.
  • .reduce({...newVariableFunctions}) - reduces a group data set and outputs new variables based on the argument passed.
  • .map(mapFunction) - functions just like the array map function, but works on both grouped and ungrouped Plys.
  • .select(fieldsOrSelectFunction) - selects columns from a Ply object.

reducer helper functions

All the reducer helper functions return a function that takes an array, pulls out all the data associated with key, then returns the output of that function.

Here's how they look in practice:

let data = [
	{facet: 'a', x: 10},
    {facet: 'b', x: 15},
    ...
]

const ply = new Ply(data)
let out = ply
  .group('facet')
  .reduce({
    avg: Ply.mean('x'),
    sd: Ply.standardDeviation('x')
  })
  .transform()

Alternatively, you can just use them directly.

let mean = Ply.mean('x')(data)

functions

  • Ply.sum(key) returns a sum.
  • Ply.mean(key) returns the mean.
  • Ply.variance(key) returns the variance, calculated via Welford's method.
  • Ply.standardDeviation(key) returns the standard deviation.
  • Ply.covariance(key1, key2) returns the covariance.
  • Ply.correlation(key1, key2) returns the correlation.
  • Ply.spearman(key1, key2) returns the spearman correlation coefficient.
  • Ply.quantile(q, key) returns the value at quantile q.
  • Ply.median(key) shorthand for Ply.quantile(.5, key).
  • Ply.IQR(key) returns the inter-quartile range.
  • Ply.mode(key) returns the mode.

Related Skills

View on GitHub
GitHub Stars10
CategoryDesign
Updated5y ago
Forks1

Languages

JavaScript

Security Score

60/100

Audited on Apr 18, 2020

No findings