1
(require '[incanter.core :as icore])

;; Assume dataset "data" is already loaded by incanter.core/read-dataset

;; Let's examine the columns (note that Volume is the 5th column)
(icore/col-names data)
==> [:Date :Open :High :Low :Close :Volume]

;; We CAN use the :Volume keyword to look at just that column
(icore/sel data :cols Volume)
==> (11886469 9367474 12847099 9938230 11446219 12298336 15985045...)

;; But we CANNOT use the :Volume keyword with filters
;; (well, not without looking up the position in col-names first...)
(icore/sel data :filter #(> (#{:Volume} %) 1000000))

显然这是因为过滤器的匿名函数正在查看 LazySeq,它不再将列名作为其结构的一部分,因此上面的代码甚至无法编译。我的问题是:Incanter 是否有办法执行此过滤查询,仍然允许我使用列关键字?例如,我可以让它工作,因为我知道 :Volume 是第 5 列

(icore/sel data :filter #(> (nth % 5) 1000000))

不过,我还是想看看 Incanter 是否有办法为这种类型的过滤查询保留 column 关键字。

4

1 回答 1

4

示例数据集:

(def data
  (icore/dataset
    [:foo :bar :baz :quux]
    [[0 0 0 0]
     [1 1 1 1]
     [2 2 2 2]]))

带有结果的示例查询:

(icore/$where {:baz {:fn #(> % 1)}} data)

| :foo | :bar | :baz | :quux |
|------+------+------+-------|
|    2 |    2 |    2 |     2 |

其实也可以这样写

(icore/$where {:baz {:gt 1}} data)

除了:gt: :lt, :lte, :gte, :eq(对应于 Clojure 的=), :ne( not=), :in, :nin(not in) 之外,还支持几个这样的“谓词关键字”。

:fn是通用的“使用任何功能”关键字。

所有这些都可以用$:$fn等)作为前缀,含义不变。

于 2014-01-07T03:39:46.723 回答