简体   繁体   中英

d3.csv modifying the input data

I am using d3.tsv to parse through a file. I want to change all the zeros in one column of the data(PValue column) to the next lowest number in that column. I believe the correct way of doing this is to use the accessor function but my attempts have so far failed.

d3.tsv(filename, modifyData, function(error, data) {

    data.forEach(function(d) {
        d.NAME = d.NAME;
        d.logFC = +d.logFC;
        d.logCPM = +d.logCPM;
        d.FDR = +d.FDR;
        d.PValue = +d.PValue
    });
})

When I try to do something like the following in the accessor function modifyData, I get an error saying 'data' is undefined in the code above.

function modifyData(d){
    d.forEach(function(origData){
        origData.PValue = +origData.PValue
        pValue_array.push(origData.PValue)
    })
    var pValue_array = [] 
    for (var i = pValue_array.length-1 ; i >= 0; i--){
        if (pValue_array[i] === 0){
            pValue_array.splice(i,1);
        }
    }
    var newPzero = (arrayMin(pValue_array))
    return d;
};

arrayMin is a simple function that returns the minimum value in an array. I was planning on using this value to replace all of the 0s in the PValue column. Help is greatly appreciated!

You can find the min value first and then replace the 0s:

d3.tsv('data.tsv', function(error, data) {

//Option A
// smallest = d3.min(data, function(d) {return +d.PValue || Infinity; })

//Option B
var noZeroes = data.filter(function(d) { return +d.Data !== 0; });
var smallest = d3.min(noZeroes, function(d) { return d.Data; })


    data.forEach(function(d) {

        d.NAME = d.NAME;
        d.logFC = +d.logFC;
        d.logCPM = +d.logCPM;
        d.FDR = +d.FDR;

        if (+d.PValue == 0 ) {
          d.Data = +smallest;
        } else {
          d.PValue = +d.PValue 
        }
    });

console.table(data);
})

Don't forget the "+" for numeric values, otherwise JS consider it as string, and your comparation will fail.-

You can use d3.min to get the minimum value from your data set.

For example

d3.tsv(filename, function(error, data) {
    data.forEach(function(d) {
        d.NAME = d.NAME;
        d.logFC = +d.logFC;
        d.logCPM = +d.logCPM;
        d.FDR = +d.FDR;

        d.PValue = +(d.PValue || d3.min(data, function(d) { return d.PValue || Infinity; }));
    });
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM