d3.js: How to import csv data with float and string types

2k views Asked by At

Very new to d3 and javascript, but have done a good four or so hours trying to resolve this issue and haven't found a solution yet.

My .csv dataset I'd like to use has both string and float numbers (see sample set here: http://individual.utoronto.ca/lannajin/d3/pcadummyset.csv), which I'm currently having issues importing.

d3.csv("http://individual.utoronto.ca/lannajin/d3/pcadummyset.csv")
    .row(function (d) {
    return {
        metric: d.metric,
        Var: d.Var,
        param: d.param,
        PC1: +d.PC1, // convert "PC1" column to number
        PC2: +d.PC2, // convert "PC2" column to number
        Type: d.Type
    };
})
    .get(function (error, rows) {
    console.log(rows);
});

Where I've tried variations (for line 7 & 8) as:

PC1: d.parseFloat("PC1"),
PC1: parseFloat(d.PC1),
PC1: d.parseFloat.PC1,

etc.

The main problem is that my float characters (PC1 and PC2) are in the form 1.0e-02 rather than 0.001, which means I can't simply use the "+" operator to convert my data to float. Instead, I have to use the parseFloat function, which, I unfortunately am not sure how to use.

When I change the structure to the solution offered by Importing CSV without headers into D3 - data with both numbers and strings,

data = d3.csv.parseRows("http://individual.utoronto.ca/lannajin/d3/pcadummyset.csv").map(function(row) {
    return row.map(function(value, index) {
        if(index == 2) {
            return value;
        } else {
            return +value;
        }
    });
});

It's clear that the data is being read, but not plotted since the PC1 and PC2 values are probably being read in as strings. I tried to integrate the parseFloat function, but it still won't plot my values.

Help please!

0

There are 0 answers