-1

I am trying to get the first quartile, the median and last quartile of an array of object base on their value.

0: Object { key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering", value: 0.6100000000000001 }
1: Object { key: "Agriculture, Veterinary and Food Science", value: 1.3400000000000003 }
2: Object { key: "Allied Health Professions, Dentistry, Nursing and Pharmacy", value: 2.2699999999999996 }
3: Object { key: "Anthropology and Development Studies", value: 1.1400000000000003 }
4: Object { key: "Architecture, Built Environment and Planning", value: 4.21 }
5: Object { key: "Area Studies", value: 0.9800000000000001 }
6: Object { key: "Art and Design: History, Practice and Theory", value: 6.659999999999997 }
7: Object { key: "Biological Sciences", value: 1.3500000000000003 }
8: Object { key: "Business and Management Studies", value: 8.409999999999993 }
9: Object { key: "Chemistry", value: 0.15000000000000002 }

I would like to return an array of object with 3 object. My final goal is to draw a pie chart with it.

so far i tried this method

        var rank = dmObj.getRank();
        console.log("test",rank);
        // using nest function from d3 to "classify" data into years
        // then rollup to change array of values into sum of money
        var nestedData = d3.nest()
            .key(function(d){ return d.naAsses;})
            .rollup(function (v) { return {
                first: d3.quantile(v.map(function(d){return rank.value ;}),.25),
                median: d3.median(v,function(d){return rank.value}),
                last: d3.quantile(v.map(function(d){return rank.value;}),.75)
            };

            })
            .sortKeys(d3.ascending)
            .entries(filteredData());

        console.log("quartil :data",nestedData);

        return nestedData;
    }

rank is the dataset linked above.

Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
Boat
  • 509
  • 3
  • 8
  • 21

1 Answers1

0

Instead of using d3.nest() you can simply use those methods in the array directly:

var data = [{
  key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering",
  value: 0.6100000000000001
}, {
  key: "Agriculture, Veterinary and Food Science",
  value: 1.3400000000000003
}, {
  key: "Allied Health Professions, Dentistry, Nursing and Pharmacy",
  value: 2.2699999999999996
}, {
  key: "Anthropology and Development Studies",
  value: 1.1400000000000003
}, {
  key: "Architecture, Built Environment and Planning",
  value: 4.21
}, {
  key: "Area Studies",
  value: 0.9800000000000001
}, {
  key: "Art and Design: History, Practice and Theory",
  value: 6.659999999999997
}, {
  key: "Biological Sciences",
  value: 1.3500000000000003
}, {
  key: "Business and Management Studies",
  value: 8.409999999999993
}, {
  key: "Chemistry",
  value: 0.15000000000000002
}];

data.sort(function(a, b) {
  return d3.ascending(a.value, b.value)
});

var firstQuartile = d3.quantile(data.map(function(d) {
  return d.value
}), 0.25);
var median = d3.median(data.map(function(d) {
  return d.value
}));
var lastQuartile = d3.quantile(data.map(function(d) {
  return d.value
}), 0.75);

console.log("First quartile: " + firstQuartile)
console.log("Median: " + median)
console.log("Last quartile: " + lastQuartile)
<script src="https://d3js.org/d3.v4.min.js"></script>

EDIT: according to your comment, you can create a new property based on the quartiles:

var data = [{
  key: "Aeronautical, Mechanical, Chemical and Manufacturing Engineering",
  value: 0.6100000000000001
}, {
  key: "Agriculture, Veterinary and Food Science",
  value: 1.3400000000000003
}, {
  key: "Allied Health Professions, Dentistry, Nursing and Pharmacy",
  value: 2.2699999999999996
}, {
  key: "Anthropology and Development Studies",
  value: 1.1400000000000003
}, {
  key: "Architecture, Built Environment and Planning",
  value: 4.21
}, {
  key: "Area Studies",
  value: 0.9800000000000001
}, {
  key: "Art and Design: History, Practice and Theory",
  value: 6.659999999999997
}, {
  key: "Biological Sciences",
  value: 1.3500000000000003
}, {
  key: "Business and Management Studies",
  value: 8.409999999999993
}, {
  key: "Chemistry",
  value: 0.15000000000000002
}];

data.sort(function(a, b) {
  return d3.ascending(a.value, b.value)
})

data.forEach(function(d, i, arr) {
  d.quartile = d.value < d3.quantile(arr.map(function(d) {
    return d.value
  }), 0.25) ? "belowFirst" : d.value > d3.quantile(arr.map(function(d) {
    return d.value
  }), 0.75) ? "aboveThird" : "betweenFirstandThird"
});

console.log(data)
<script src="https://d3js.org/d3.v4.min.js"></script>
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
  • Well maybe I could not use a nest . But for the result I would like to have the key name instead of a value. – Boat Nov 17 '17 at 04:01
  • 1
    *"But for the result I would like to have the key name instead of a value"* What does that mean? The quartiles are calculated taking into accoun **all** the values. – Gerardo Furtado Nov 17 '17 at 04:08
  • Well i want to classify the object key in function of the value. e.g Chemistry and art and design are in the first quartile, business in the second.. etc – Boat Nov 17 '17 at 05:06
  • @Boat check my edit. Have in mind that there is no such a thing like *being in the first quartile*: the quartiles are numbers, not ranges. – Gerardo Furtado Nov 17 '17 at 06:44