-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatascript.js
77 lines (70 loc) · 2.41 KB
/
datascript.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fs from 'fs';
import csv from 'csv-parser';
import { mean } from './functions.js'; // Custom functions module
class DataScript {
constructor() {
this.data = {};
}
async loadCSV(file, variable) {
const results = [];
return new Promise((resolve, reject) => {
fs.createReadStream(file)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
this.data[variable] = results;
console.log(`Loaded ${file} into ${variable}`);
resolve();
})
.on('error', reject);
});
}
async summarize(variable) {
const data = this.data[variable];
if (!data) {
console.log(`No data found for variable: ${variable}`);
return;
}
// Implement summary logic (e.g., mean, count, etc.)
// Example:
const summary = {
count: data.length,
mean_age: mean(data.map(row => parseFloat(row.age))),
// Add more statistics as needed
};
console.log(`Summary of ${variable}:`, summary);
}
filter(variable, condition, newVariable) {
const data = this.data[variable];
if (!data) {
console.log(`No data found for variable: ${variable}`);
return;
}
const filteredData = data.filter(row => eval(condition));
this.data[newVariable] = filteredData;
console.log(`Filtered ${variable} into ${newVariable}`);
}
groupBy(variable, column, newVariable) {
const data = this.data[variable];
if (!data) {
console.log(`No data found for variable: ${variable}`);
return;
}
const groupedData = data.reduce((groups, row) => {
const key = row[column];
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(row);
return groups;
}, {});
this.data[newVariable] = groupedData;
console.log(`Grouped ${variable} by ${column} into ${newVariable}`);
}
async plot(variable, type, options) {
// Implement plotting logic (e.g., using a plotting library)
console.log(`Plotting ${type} of ${variable} with options:`, options);
// Placeholder for actual plotting code
}
}
export default DataScript;