What is the idiomatic way to get a column sequence from an incanter dataset?

What is the best way to get a sequence of columns (like vectors or something else) from an Incanter dataset?

I thought:

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

But ideally, I would like a lazy consistency. Is there a better way?

+5
source share
3 answers

Use a macro $.

=> (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
+5
source

Looking at the source code to-vect, he uses it mapto create a result that already provides one degree of laziness. Unfortunately, it looks like the entire dataset is first transformed toArray, perhaps just discarding all the benefits of maplazyness.

, , , gora Java-, to-vect.

+2

.

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))

Although I'm not sure how internal the API is. You should probably check this out with the kids.

+1
source

All Articles