How to get quantile of object array

爷,独闯天下 提交于 2020-01-14 01:49:18

问题


I have an object array, called dataArray, where one object is like this:

{
   id,
   x,
   y
}

now I want to have the lower quantile of this array by using d3. I thought about something like this:

quartile = d3.quantile(dataArray, 0.25, function(d) {return d.y})

but unfortunately there is no accessor option for this method. How can I get the quantile of a property from an object array ? Thanks in advance


回答1:


I found the solution to my problem. I need to get an array of only a certain property of an array of objects, so I don't need an accessor within the quantile method.

// get an array of just the y property values
var keyArray = dataArray.map(function(item) { return item["y"]; });

// calculate a lower quantile of this array
var quantile_y = d3.quantile(keyArray, 0.25);

UPDATE:

In order to work correctly, the array given to d3.quantile() has to be sorted. e.g.

keyArray.sort(d3.ascending);


来源:https://stackoverflow.com/questions/31612640/how-to-get-quantile-of-object-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!