Reputation: 2409
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!
Upvotes: 0
Views: 957
Reputation: 1836
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.-
Upvotes: 1
Reputation: 1728
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; }));
});
})
Upvotes: 0