CrossFilter/JS newbie here.
This question pretty much describes exactly what I'm trying to do but there doesn't seem to be a solution using CrossFilter:
How to return the number of unique values by category using crossfilter?
I have data with
var va = [{
date: "2014-10-01",
id: "1"},
{
date: "2014-10-02",
id: "1"},
{
date: "2014-10-03",
id: "1"},
{
date: "2014-10-04",
id: "1"},
{
date: "2014-10-05",
id: "1"},
{
date: "2014-10-01",
id: "2"},
{
date: "2014-10-02",
id: "2"},
{
date: "2014-10-03",
id: "2"},
{
date: "2014-10-04",
id: "1"},
{
date: "2014-10-01",
id: "3"},
{
date: "2014-10-02",
id: "3"},
{
date: "2014-10-03",
id: "1"},
{
date: "2014-10-01",
id: "4"},
{
date: "2014-10-02",
id: "1"},
{
date: "2014-10-01",
id: "5"}
}
I am trying to get the number of unique id's per date from this. I would like to group by date and basically have a count of unique id's for that particular date:
"2014-10-01" - 5
"2014-10-02" - 3
"2014-10-03" - 2
"2014-10-04" - 1
"2014-10-05" - 1
Currently, I'm trying to follow the answer given in this question
Crossfilter reduce :: find number of uniques
to do the following:
//Create a Crossfilter instance
var ndx = crossfilter(va);
//Define dimensions
var date_dim = ndx.dimension(function(d) {
return d["date"]; });
//total number of ids per date
var num_ids_by_date = date_dim.group();
//unique number of ids per date
var num_uniq_ids_by_date = date_dim
.group()
.reduce(
function (p, d) {
if(d.id in p.ids){
}
else{
p.ids[d.id] = 1;
}
return p;
},
function (p, d) {
p.ids[d.id]--;
if(p.ids[d.id] === 0){
delete p.ids[d.id];
}
return p;
},
function () {
return {ids: {}};
})
When I look in the num_uniq_ids_by_date object and call num_uniq_ids_by_date.reduceCount().top(1), it seems to be the same output as num_ids_by_date.top(1).
So, I still don't seem to be getting what I'm looking for and have been stumped for a while.
Any suggestions? Thanks in advance!
num_uniq_ids_by_date.reduceCount()wipes out all your custom group reducers. Just callnum_uniq_ids_by_date.top(1). - Ethan Jewett