0

I'm using d3.nest.rollup to count leaves in my data. The data comes from csv and is converted to json by d3. The csv looks like this:

Color,ID,Animal
Green,1,Dog
Red,2,Cat
Red,3,Cat
Red,3,Dog

Note that ID #3 has 2 different animals, but I want to count distinct IDs in this data, so each color should return count = 1.

Simple rollup function would return counts of all the leaves, i.e. 1, 1, 2, whereas I need to group by color and count distinct IDs within that group...

Henry S
  • 3,072
  • 1
  • 13
  • 25
Pavel
  • 53
  • 1
  • 2
  • 8

1 Answers1

0

You'll need to do a two-level next (first on color, then on id), and roll-up on the second group, something like this:

var nested_data = d3.nest()
.key(function(d) { return d.Color; })
.key(function(d) { return d.id; })
.rollup(function(ids) {
    return ids.length; 
})
.entries(data);

console.log(nested_data);

which will give you:

Console view of rolled-up and nested data

There's a great tutorial on nest and rollup here: http://bl.ocks.org/phoebebright/raw/3176159/

Henry S
  • 3,072
  • 1
  • 13
  • 25
  • Thanks! But this still gives me length = 2 for Red... whereas I need to get length = 1 for Red as there is only one unique ID under Red group... – Pavel Jan 18 '16 at 13:51
  • Sorry, I don't quite follow. Using the CSV example in the question, "Green" has one unique ID ("1"), and "Red" has two unique IDs ("2" and "3"). Am I missing something? – Henry S Jan 18 '16 at 13:55
  • Right! I can't seem to replicate this on the actual data, which has many other columns... do I need to include the ID column in the keys or can I somehow bypass it? – Pavel Jan 18 '16 at 15:17
  • On the actual data under Object a "value" attribute gets written, and it unfortunately counts all rows (not just the distinct IDs)... what can I still be doing wrong? – Pavel Jan 18 '16 at 15:22
  • Hi. You will need to modify `return d.Color;` and `return d.id;` to match the names of the first and second keys that you want to nest over. If you have a snippet of your actual data, you may want to add it to the question (or ask a separate one) – Henry S Jan 19 '16 at 17:41
  • I found a way to get unique values from the full data set: var m = d3.map(data, function(d) { return d.id; }).size(); This returns count of unique ids, but now I'd like to integrate it into .rollup part of the code so that .rollup puts the unique count of ids into the "values" property instead of ids.length, which would count all the rows... – Pavel Jan 20 '16 at 16:26
  • Hi @pavel, how about trying the following inside the nest: `var nest = d3.nest() .key(d => d.id) .rollup(leaves => d3.map(leaves, d => d.id).size()) .entries(data)` – ranaalisaeed Jun 03 '20 at 00:33