-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2b_numeric-data-in-column-name.js
46 lines (37 loc) · 1.18 KB
/
2b_numeric-data-in-column-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const io = require('indian-ocean');
/* --------------------------------------------
*
* Not yet implemented
*
* --------------------------------------------
*/
function pivot() {
}
/* --------------------------------------------
* relig_income example
*/
const religIncome = io.readDataSync('./tidyr/data-raw/relig_income.csv');
// could also do second argument as `(col, i) => i > 0;
const religIncomePivoted = pivot()
.columns(religIncome.columns.slice(1))
.names(['income'])
.values(['count'])(religIncome);
console.log(religIncomePivoted);
/* --------------------------------------------
* Billboard example
*/
const toInt = d => +d;
const billboard = io.readDataSync('./tidyr/data-raw/billboard.csv');
// Maybe also support regex for column names in second argument
// The second argument could also be a filter function on the `data.columns`
const billboardPivoted = pivot()
.columns(col => col.startsWith('wk'))
.names(['week'])
// transforms are matched up by index
.transformNames([d => +d.replace('wk', '')])
.values(['rank'])
.transformValues([toInt])
.options({
filter: d => d.rank !== null // filter long format data
})(billboard);
console.log(billboardPivoted);