1

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!

Community
  • 1
  • 1
user1301593
  • 631
  • 1
  • 6
  • 16
  • So what's the issue? – Lars Kotthoff Nov 19 '14 at 15:51
  • It doesn't import the data. – user1301593 Nov 19 '14 at 15:55
  • When I do something in situ: http://stackoverflow.com/questions/16417252/importing-csv-without-headers-into-d3-data-with-both-numbers-and-strings?rq=1 it appears to be doing something, but I suspect the data is still not being read in properly. The main issue is that my values are encoded as 1.0e-02 instead of 0.001, which means I can't use the "+", but have to use "parseFloat". But the parseFloat doesn't yield any response, so I think I'm using parseFloat incorrectly. – user1301593 Nov 19 '14 at 15:59
  • What do you mean when you say that you don't get any response? Do you get any error messages? The link to your example data doesn't work, did you check whether it's loaded correctly? – Lars Kotthoff Nov 19 '14 at 16:17
  • Whoops, fixed the link. Here: http://jsfiddle.net/lannajin/2hfx3/34/ – user1301593 Nov 19 '14 at 16:22
  • So, here's what the output will look like if it's loaded properly: http://jsfiddle.net/lannajin/2hfx3/35/ As you can see, I've added imported some junk data, but with the same variables as the .csv I would like to ideally import. – user1301593 Nov 19 '14 at 16:25
  • Well, parsing the numbers in your CSV works fine for me even with just `+` -- are you sure that the issue is the parsing of the numbers? – Lars Kotthoff Nov 19 '14 at 18:19
  • I think d3.csv.parseRows(string) expects a string of CSV contents, not a URL? – rtlechow Nov 19 '14 at 18:41
  • 2
    Think this does the trick: http://jsfiddle.net/rtlechow/2hfx3/41/ Props to http://stackoverflow.com/questions/16139452/how-to-convert-big-negative-scientific-notation-number-into-decimal-notation-str for the heavy lifting! – rtlechow Nov 19 '14 at 19:01

0 Answers0