简体   繁体   English

d3.csv修改输入数据

[英]d3.csv modifying the input data

I am using d3.tsv to parse through a file. 我正在使用d3.tsv来解析文件。 I want to change all the zeros in one column of the data(PValue column) to the next lowest number in that column. 我想将data(PValue列)的一列中的所有零更改为该列中的下一个最低编号。 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. 当我尝试在访问器函数ModifyData中执行类似以下操作时,出现错误,表明上面的代码中未定义“数据”。

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. arrayMin是一个简单的函数,它返回数组中的最小值。 I was planning on using this value to replace all of the 0s in the PValue column. 我打算使用该值替换PValue列中的所有0。 Help is greatly appreciated! 非常感谢您的帮助!

You can find the min value first and then replace the 0s: 您可以先找到最小值,然后替换为0:

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.- 不要忘记数字值的“ +”,否则JS会将其视为字符串,您的比较将会失败。

You can use d3.min to get the minimum value from your data set. 您可以使用d3.min从数据集中获取最小值。

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; }));
    });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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