我有如下例子的数据。第一列是id,第二列是v1,第三列是v2。
数据var v1Func = d3.random.normal(100, 15)
var v2Func = d3.random.normal(200, 30)
var sample = {
id: [],
v1: [],
v2: []
}
for (var i = 0; i < 100; i++) {
sample.id.push(i+1);
sample.v1.push(v1Func());
sample.v2.push(v2Func());
}
我的问题是如何计算列v1和v2的平均值,然后像这样转换数据表:
name mean
v1 *
v2 *
提前致谢!
答案 0 :(得分:1)
我不完全确定您要求的内容,但以下内容会根据您的需要为您提供数据对象:
var v1Func = d3.random.normal(100, 15)
var v2Func = d3.random.normal(200, 30)
var sample = {
id: [],
v1: [],
v2: [],
}
for (var i = 0; i < 100; i++) {
sample.id.push(i+1);
sample.v1.push(v1Func());
sample.v2.push(v2Func());
}
var totalv1 = 0;
var totalv2 = 0;
for (var i =0; i < sample.v1.length; i++) {
totalv1 += sample.v1[i];
totalv2 += sample.v2[i];
}
var averagev1 = totalv1/sample.v1.length;
var averagev2 = totalv2/sample.v2.length;
var dataTable = [
{
name: "v1",
mean: averagev1
},
{
name: "v2",
mean: averagev2
}
];