Skip to content Skip to sidebar Skip to footer

Combining Two Csv's (d3)

I am loading two csv files using the following code d3.csv('sqrt100train.csv', function(error, data2) { d3.csv('sqrt100test.csv', function(error, data) { sqrt100train look

Solution 1:

Problem: the length or the X value are different

serie1
X,  Y
1,  11
3,  13
5,  15
6,  16

serie2
X,  Y
1,  21
2,  22
4,  24
6,  26
7,  27

BothSeries
X,  Y1, Y2
1,  11, 21
2,    , 22
3,  13, 
4,    , 24
5,  15, 
6,  16, 26
7,    , 27

This code fix it.

varserie1= [];
varserie2= [];
varBothSeries= [[null,null,null]];


function merge() {
    BothSeries = [];
    var i, j, aux;
    //copy serie1 a BothSeriesif(serie1.length!=0){
        for (vari=0; i < serie1.length; i++) {
            //null represent the serie2 value
            BothSeries[i] = [serie1[i][0], serie1[i][1],null];
        }
    }
    //set the value Y of serie2 in the BothSeries when their X are equals.if(serie2.length!=0){
        for (vari=0; i < serie2.length; i++) {
            for (varj=0; j < BothSeries.length; j++) {
                if(BothSeries[j][0] == serie2[i][0]){
                    BothSeries[j][2] = serie2[i][1];
                }
            }
        }
    }
    //set the value X and Y of serie2 in the BothSeries when their X aren't equals or doesn't exist.var cont=0;
    if(serie2.length!=0){
        for (vari=0; i < serie2.length; i++) {
            for (varj=0; j < BothSeries.length; j++) {
                if(BothSeries[j][0] != serie2[i][0]){
                    cont++;
                }
            }
            if(cont==BothSeries.length)
            {
                BothSeries[BothSeries.length] = [serie2[i][0],null,serie2[i][1]];
            }
            cont=0;
        }
    }
    //order with bubble method by Xif(BothSeries.length!=0){
        for(i=0;i<BothSeries.length-1;i++){
            for(j=0;j<BothSeries.length-i-1;j++){
                if(BothSeries[j+1][0] < BothSeries[j][0])
                {
                    aux=BothSeries[j+1];
                    BothSeries[j+1]=BothSeries[j];
                    BothSeries[j]=aux;
                }
            }
        }
    }
}

Solution 2:

If you want to create new array:

var newArray = [];
for(var i = 0; i < data.length; i++) {
    newArray.push({Trees: data[i].Trees,
                   Train: data[i].Train,
                   Test: data2[i].Test});
}

Post a Comment for "Combining Two Csv's (d3)"