例如,如果我們有書籍,作者和日期信息數據。我們是否可以爲每個作者每月有多少書作爲交叉過濾器?在crossfilter中可以按多個維度進行分組嗎?
6
A
回答
21
在僞SQL來說,你正在嘗試做的是:
SELECT COUNT(book)
GROUP BY author, month
我走近這類問題的辦法是「組」的字段一起到一個單一的維度。所以在你的情況下,我會將月份和作者信息連接在一起,形成一個維度。
讓這成爲我們的測試數據:
var cf = crossfilter([
{ date:"1 jan 2014", author: "Mr X", book: "Book 1" },
{ date:"2 jan 2014", author: "Mr X", book: "Book 2" },
{ date:"3 feb 2014", author: "Mr X", book: "Book 3" },
{ date:"1 mar 2014", author: "Mr X", book: "Book 4" },
{ date:"2 apr 2014", author: "Mr X", book: "Book 5" },
{ date:"3 apr 2014", author: "Mr X", book: "Book 6"},
{ date:"1 jan 2014", author: "Ms Y", book: "Book 7" },
{ date:"2 jan 2014", author: "Ms Y", book: "Book 8" },
{ date:"3 jan 2014", author: "Ms Y", book: "Book 9" },
{ date:"1 mar 2014", author: "Ms Y", book: "Book 10" },
{ date:"2 mar 2014", author: "Ms Y", book: "Book 11" },
{ date:"3 mar 2014", author: "Ms Y", book: "Book 12" },
{ date:"4 apr 2014", author: "Ms Y", book: "Book 13" }
]);
尺寸定義如下:
var dimensionMonthAuthor = cf.dimension(function (d) {
var thisDate = new Date(d.date);
return 'month='+thisDate.getMonth()+';author='+d.author;
});
而現在我們只要簡單地做一個減少數來計算多少書有每作者,每月(即每個維度單位):
var monthAuthorCount = dimensionMonthAuthor.group().reduceCount(function (d) { return d.book; }).all();
並且結果如下:
{"key":"month=0;author=Mr X","value":2}
{"key":"month=0;author=Ms Y","value":3}
{"key":"month=1;author=Mr X","value":1}
{"key":"month=2;author=Mr X","value":1}
{"key":"month=2;author=Ms Y","value":3}
{"key":"month=3;author=Mr X","value":2}
{"key":"month=3;author=Ms Y","value":1}
4
我沒有找到接受的答案都有幫助。
我用下面的代替。
我第一次做了一個鍵組(在你的情況下本月)
var authors = cf.dimension(function (d) {
return +d['month'];
})
接下來,我用的地圖減輕鍵數據集的方法來計算平均
分組輔助功能:
var monthsAvg = authors.group().reduce(reduceAddbooks, reduceRemovebooks, reduceInitialbooks).all();
地圖-降低功能:
function reduceAddbooks(p, v) {
p.author = v['author'];
p.books = +v['books'];
return p;
}
function reduceRemovebooks(p, v) {
p.author = v['author'];
p.books = +v['books'];
return p;
}
function reduceInitialbooks() {
return {
author:0,
books:0
};
}
3
我想更新舊的答案與周圍描述的新作:https://github.com/dc-js/dc.js/pull/91
這一業績尚未在大型數據集測試
var cf = crossfilter([
{ date:"1 jan 2014", author: "Mr X", book: "Book 1" },
{ date:"2 jan 2014", author: "Mr X", book: "Book 2" },
{ date:"3 feb 2014", author: "Mr X", book: "Book 3" },
{ date:"1 mar 2014", author: "Mr X", book: "Book 4" },
{ date:"2 apr 2014", author: "Mr X", book: "Book 5" },
{ date:"3 apr 2014", author: "Mr X", book: "Book 6"},
{ date:"1 jan 2014", author: "Ms Y", book: "Book 7" },
{ date:"2 jan 2014", author: "Ms Y", book: "Book 8" },
{ date:"3 jan 2014", author: "Ms Y", book: "Book 9" },
{ date:"1 mar 2014", author: "Ms Y", book: "Book 10" },
{ date:"2 mar 2014", author: "Ms Y", book: "Book 11" },
{ date:"3 mar 2014", author: "Ms Y", book: "Book 12" },
{ date:"4 apr 2014", author: "Ms Y", book: "Book 13" }
]);
var dimensionMonthAuthor = cf.dimension(function (d) {
var thisDate = new Date(d.date);
//stringify() and later, parse() to get keyed objects
return JSON.stringify ({ date: thisDate.getMonth() , author: d.author }) ;
});
group = dimensionMonthAuthor.group();
//this forEach method could be very expensive on write.
group.all().forEach(function(d) {
//parse the json string created above
d.key = JSON.parse(d.key);
});
return group.all()
結果:
[ { key: { date: 0, author: 'Mr X' },
value: 2 },
{ key: { date: 0, author: 'Ms Y' },
value: 3 },
{ key: { date: 1, author: 'Mr X' },
value: 1 },
{ key: { date: 2, author: 'Mr X' },
value: 1 },
{ key: { date: 2, author: 'Ms Y' },
value: 3 },
{ key: { date: 3, author: 'Mr X' },
value: 2 },
{ key: { date: 3, author: 'Ms Y' },
value: 1 } ]
相關問題
- 1. 在SPGridView中可以進行多個分組嗎?
- 2. 如何按維度級別對數組進行分組?
- 3. 總是可以在所有維度中訂購多維數組嗎?怎麼樣?
- 4. 我可以按不在SELECT行中的內容進行分組嗎?
- 5. 按兩個值分組多維數組
- 6. 如何按範圍對自定義維度值進行分組
- 7. 在PHP中對多維數組進行分組
- 8. 可以使用二維數組進行數字計數嗎?
- 9. matlab:可以在多個組中進行觀察的分組變量
- 10. RethinkDB:我可以按日期有效地按字段進行分組嗎?
- 11. 我可以在Tableau中同時在兩個組下放置維度值嗎?
- 12. 我可以在awk中進行基於時間的進度嗎?
- 13. 您可以在Flex中使用多個軸標籤分組嗎?
- 14. 我可以在CSS中分組多個樣式聲明嗎?
- 15. 基於兩個維度對JavaScript中的多維數組進行排序?
- 16. 可以在Shell中測量進度嗎?
- 17. 在多個維度上分組在Teradata中
- 18. 多維數組中單個維度(可以變化)的數據選擇
- 19. jQuery Tablesorter - 可以對'排序'行進行分組嗎?
- 20. 這個長矩陣可以在代碼中分成多行嗎?
- 21. 在Talend中可以有多個行分隔符嗎?
- 22. Python:按數組長度對二維數組進行排序?
- 23. PHP按3個值對多維數組進行排序
- 24. 按兩個標準對多維數組進行排序?
- 25. 以下多維數組中可以存儲多少個整數?
- 26. 可變長度參數可以代替單維數組嗎?
- 27. 按特定值分組多維數組
- 28. 按特定值分組多維數組?
- 29. 多維數組按月分組
- 30. 按多列和時間跨度對MYSQL結果進行分組