-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-6.9.js
42 lines (38 loc) · 1.11 KB
/
listing-6.9.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
//
// This example imports a CSV file, filters out an entire column and exports a new CSV file.
//
// This example uses Data-Forge.
//
"use strict";
const dataForge = require('data-forge');
const inputFileName = "./data/surveys.csv";
const outputFileName = "./output/surveys-aggregated.csv";
function transformData (inputDataFrame) {
return inputDataFrame
.parseFloats("transects_length")
.groupBy(inputRow => inputRow.reef_name)
.select(group => {
return {
reef_name: group.first().reef_name,
transects_length: group
.deflate(row => row.transects_length)
.sum(),
};
})
.inflate();
}
dataForge.readFile(inputFileName)
.parseCSV()
.then(inputDataFrame => {
const outputDataFrame = transformData(inputDataFrame);
return outputDataFrame
.asCSV()
.writeFile(outputFileName);
})
.then(() => {
console.log("Done!");
})
.catch(err => {
console.error("Error!");
console.error(err && err.stack || err);
});