0.1.0 • Published 4 years ago

tidydata v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

tidydata-ts

tidydata-ts aims at easing data manipulation with javascript. It is inspired by Hadley Wickham's tidyverse R package and reproduces its API.

So far, one function is implemented:

  • pivot_longer: converts an array of objects from a wider format to a longer format.
    • data: the array to pivot
    • cols: an array of field names
    • name_to: name of the added field

The returned objects will also have an added property value with the value of the pivoted fields.

Wider and Longer data table

Wider data table

In the mock data table below, years are used as column names, when they actually belong to the same dimension "year". The rows contains several values (one for each year).

namegender201720182019
Argentinafemale3473
Armeniafemale14.61219.8
Argentinamale543260
Armeniamale431225

Longer Data table

This longer data table contains the same information, but the years are stored in a unique column. The values are also stored in a newly created column "values". Each row contains a single observation.

namegenderyearvalue
Argentinafemale201734
Armeniafemale201714.6
Argentinamale201754
Armeniamale201743
Argentinafemale20187
Armeniafemale201812
Argentinamale201832
Armeniamale201812
Argentinafemale20193
Armeniafemale201919.8
Argentinamale201960

pivot_longer converts a data table from the wider format to the longer format.

Example

import { pivot_longer } from "tidydata";

const wider = [
  {
    country: "Argentina",
    gender: "female",
    2015: 0.3,
    2016: 0.7,
    2017: 0.7
  },
  {
    country: "Armenia",
    gender: "female",
    2015: 7.2,
    2016: 7.7,
    2017: 7.6
  }
];

const longer = pivot_longer({
  data: wider,
  cols: [2015, 2016, 2017],
  name_to: "year"
})

console.log(longer);
// [
//   {
//     "country": "Argentina",
//     "gender": "female",
//     "year": 2015,
//     "value": 0.3
//   },
//   {
//     "country": "Armenia",
//     "gender": "female",
//     "year": 2015,
//     "value": 7.2
//   },
//   {
//     "country": "Argentina",
//     "gender": "female",
//     "year": 2016,
//     "value": 0.7
//   },
//   {
//     "country": "Armenia",
//     "gender": "female",
//     "year": 2016,
//     "value": 7.7
//   },
//   {
//     "country": "Argentina",
//     "gender": "female",
//     "year": 2017,
//     "value": 0.7
//   },
//   {
//     "country": "Armenia",
//     "gender": "female",
//     "year": 2017,
//     "value": 7.6
//   }
//  ]
0.1.0

4 years ago