从incanter数据集中获取一系列列的惯用方法是什么?

时间:2011-03-30 04:25:59

标签: clojure incanter

从Incanter数据集获取一系列列(作为向量或其他)的最佳方法是什么?

我想到了:

(to-vect (trans (to-matrix my-dataset)))

但理想情况下,我想要一个懒惰的序列。还有更好的方法吗?

3 个答案:

答案 0 :(得分:5)

使用$宏。

=> (def data (to-dataset [{:a 1 :b 2} {:a 3 :b 4}]))
=> ($ :a data)  ;; :a column
=> ($ 0 :all data) ;; first row

=> (type ($ :a data))
clojure.lang.LazySeq

答案 1 :(得分:2)

查看to-vect的源代码,它使用map来构建结果,这已经提供了一定程度的惰性。不幸的是,看起来整个数据集首先被转换toArray,可能只是放弃了map懒惰的所有好处。

如果你想要更多,你可能需要深入研究Java object的血腥细节,有效地保存数据集的矩阵版本并编写你自己的to-vect版本。

答案 2 :(得分:1)

您可以使用数据集的内部结构。

user=> (use 'incanter.core)
nil
user=> (def d (to-dataset [{:a 1 :b 2} {:a 3 :b 4}]))
#'user/d
user=> (:column-names d)
[:a :b]
user=> (:rows d)
[{:a 1, :b 2} {:a 3, :b 4}]
user=> (defn columns-of
         [dataset]
         (for [column (:column-names dataset)]
           (map #(get % column) (:rows dataset))))
#'user/columns-of
user=> (columns-of d)
((1 3) (2 4))

虽然我不确定内部结构在多大程度上是公共API。你可能应该和那些笨蛋一起检查一下。

相关问题