Filter rows from d3.csv containing a NaN in ANY column

1,189 views
Skip to first unread message

Jim Gwilliam

unread,
May 29, 2013, 12:15:02 AM5/29/13
to d3...@googlegroups.com
I have a dataset and a nicely working scatter plot: http://pac12statcentral.com/testsite/temp/scatter/public_html/

I'm using d3.csv to pull in a data, and each column represents one variable displayed on each of the two axes.

However, you'll notice that when I set X: to "Safeties Given Up", that Many of the points move off the scale, because there are several NaN values in that particular column.

Is there a way to filter to remove the rows in which either of the columns contain a NaN value?

For example:

if x = [1 2 3 4 NaN] and y = [6 NaN 8 9 10], I would get a result of x = [1 3 4] and y = [6 8 9] ?


I've tried:

var pts = svg.selectAll("circle").data(data).filter(function(d) {return !isNaN(d[axes.Axis]); });

But that doesn't work for me.

Thanks,
JIm

Harry Voorhees

unread,
May 29, 2013, 12:09:04 PM5/29/13
to d3...@googlegroups.com
d3.csv returns an array of keyword/values as described here, https://github.com/mbostock/d3/wiki/CSV#wiki-parse , so you need to filter the result by applying a filtering function that considers each keyword/value like this:

function ( row ) {
  return !d3.entries(row).some( 
    function( keyValue ) {
      return ( isRequiredKey( keyValue.key ) && isNaN( keyValue.value ) )
    }
  );
};

where isRequiredKey() return true for each key you want to filter on (or omit if you want to filter on all columns).

Note that in your example, a majority of the values of n_safety_given_up_o are NaN, so you might want to drop the column instead of the rows.

Harry
Reply all
Reply to author
Forward
0 new messages