gpt4 book ai didi

javascript - 使用javascript将 'long'数据转换为 'wide'数组

转载 作者:行者123 更新时间:2023-11-29 15:36:34 25 4
gpt4 key购买 nike

在 R 中,有一种非常方便的方法可以将“长”数据转换为“宽”数据,使用 {reshape2 } 包。我想在 javascript 中使用类似的转换。一个 R 示例(带 sample data ):

library(reshape2)
data.long = read.table("so_question2.csv", sep = "\t")

head(data.long)
# This is how a first few rows of the original data look like:
# dpt sem
# 15 Dpt.4 2014.1
# 16 Dpt.4 2014.1
# 17 Dpt.4 2014.1
# 18 Dpt.4 2014.1
# 19 Dpt.4 2014.1
# 20 Dpt.4 2014.1

data.wide = dcast(data, sem ~ dpt, length)

head(data.wide)
# This is how it looks like after the transformation; there are counts in columns now
# sem Dpt.1 Dpt.2 Dpt.3 Dpt.4
# 1 2012.1 0 0 0 8
# 2 2012.2 3 6 0 21
# 3 2013.1 3 4 0 29
# 4 2013.2 5 1 2 39
# 5 2014.1 5 3 7 39

我为什么需要这个:我正在学习使用 Google Charts,并希望能够在 importing them 之后转换数据从电子表格。我知道我可以在电子表格本身中进行转换;但是用大量的公式把电子表格弄得乱七八糟会很不方便。我更希望每个图表在自己的脚本中进行自己的转换。

最佳答案

我遇到了完全相同的问题!这里有一个方便的函数。

// helper function
function addIfMissing(array, value) {
var found = false;
for(var i = 0; i < array.length; i++)
if(array[i] === value)
return array;
array.push(value);
return array;
}

function restructure(input) {
var output = [], headerX = [], headerY = [], xCoor, yCoor;

// first create non-repeating headers
headerX.push(input[0][0]);
headerY.push(input[0][0]);
for(var i = 1; i < input.length; i++)
headerX = addIfMissing(headerX, input[i][0]), headerY = addIfMissing(headerY, input[i][1]);

// put headers into output array
for(var i = 0; i < headerX.length; i++)
output.push([headerX[i]]);
output[0] = headerY;

// find correct headers on both axes and input data
for(var i = 1; i < input.length; i++) {
for(var k = 1; k < headerX.length; k++)
if(output[k][0] == input[i][0])
xCoor = k;
for(var j = 1; j < headerY.length; j++)
if(output[0][j] == input[i][1])
yCoor = j;
output[xCoor][yCoor] = input[i][2];
}

return output;
}

我用这个测试了它:

var input = [
['Season', 'Type', 'Number'],
['Winter', 'Sales', 1000],
['Winter', 'Expenses', 400],
['Winter', 'Profit', 200],
['Spring', 'Sales', 1170],
['Spring', 'Expenses', 460],
['Spring', 'Profit', 250],
['Summer', 'Sales', 660],
['Summer', 'Expenses', 1120],
['Summer', 'Profit', 300],
['Fall', 'Sales', 1030],
['Fall', 'Expenses', 540],
['Fall', 'Profit', 350]
];

var desiredOutput = [
['Season', 'Sales', 'Expenses', 'Profit'],
['Winter', 1000, 400, 200],
['Spring', 1170, 460, 250],
['Summer', 660, 1120, 300],
['Fall', 1030, 540, 350]
];

var output = restructure(input);

if (JSON.stringify(output) == JSON.stringify(desiredOutput))
alert("Pass");

Test example JSFiddle

And example of how I implemented it with google charts

关于javascript - 使用javascript将 'long'数据转换为 'wide'数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com