0

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!

2
  • You need the min value exclude 0, look here: stackoverflow.com/questions/29261994/…. With the min value just replace when 0 appear Commented Jun 30, 2016 at 20:14
  • @Klaujesi Yes, I am having problems with the replacing the 0s part. Commented Jun 30, 2016 at 20:33

2 Answers 2

1

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.-

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked great when I replaced d.Data with d.PValue.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.