Database Reference
In-Depth Information
Getting ready
We'll just need Incanter listed in our project.clj ile:
(defproject inc-dsets "0.1.0"
:dependencies [[org.clojure/clojure "1.6.0"]
[incanter "1.5.5"]])
We'll also need to include this in our script or REPL:
(use 'incanter.core)
How to do it…
The primary function used to convert data into a dataset is to-dataset . While it can convert
single, scalar values into a dataset, we'll start with slightly more complicated inputs.
1.
Generally, you'll be working with at least a matrix. If you pass this to to-dataset ,
what do you get?
user=> (def matrix-set (to-dataset [[1 2 3] [4 5 6]]))
#'user/matrix-set
user=> (nrow matrix-set)
2
user=> (col-names matrix-set)
[:col-0 :col-1 :col-2]
2.
All the data's here, but it can be labeled in a better way. Does to-dataset handle
maps?
user=> (def map-set (to-dataset {:a 1, :b 2, :c 3}))
#'user/map-set
user=> (nrow map-set)
1
user=> (col-names map-set)
[:a :c :b]
3.
So, map keys become the column labels. That's much more intuitive. Let's throw a
sequence of maps at it:
user=> (def maps-set (to-dataset [{:a 1, :b 2, :c 3},
{:a 4, :b 5, :c 6}]))
#'user/maps-set
user=> (nrow maps-set)
2
user=> (col-names maps-set)
[:a :c :b]
 
Search WWH ::




Custom Search