1.2.3-SNAPSHOT permalink Arrow_down_16x16

query-dataset

incanter.core

  • (query-dataset data query-map)
Queries the given dataset using the query-map, returning a new dataset.
The query-map uses the the dataset's column-names as keys and a
simple variant of the MongoDB query language.

For instance, given a dataset with two columns, :x and :category, to query
for rows where :x equals 10, use the following query-map: {:x 10}.

To indicate that :x should be between 10 and 20, use {:x {:$gt 10 :$lt 20}}.

To indicate that :category should also be either :red, :green, or :blue, use :$in
{:x {:$gt 10 :$lt 20} :y {:$in #{:green :blue :red}}}

And to indicate that :category should not include :red, :green, or :blue, use :$nin
{:x {:$gt 10 :$lt 20} :y {:$nin #{:green :blue :red}}}

The available query terms include :$gt, :$lt, :$gte, :$lte, :$eq, :$ne, :$in, :$nin, $fn.

A row predicate function can be used instead of a query-map. The function must accept
a map, representing a row of the dataset, and return a boolean value indicating whether
the row should be included in the new dataset.

Examples:
(use '(incanter core datasets))
(def cars (get-dataset :cars))

(view (query-dataset cars {:speed 10}))
(view (query-dataset cars {:speed {:$in #{17 14 19}}}))
(view (query-dataset cars {:speed {:$lt 20 :$gt 10}}))
(view (query-dataset cars {:speed {:$fn #(> (log %) 3)}}))

;; use a row predicate function instead of a query map.
(view (query-dataset cars (fn [row] (> (/ (row "speed") (row "dist")) 1/2))))

0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/core.clj:1246 top

(defn query-dataset 
  "Queries the given dataset using the query-map, returning a new dataset.
    The query-map uses the the dataset's column-names as keys and a
    simple variant of the MongoDB query language. 

    For instance, given a dataset with two columns, :x and :category,  to query 
    for rows where :x equals 10, use the following query-map: {:x 10}.
    
    To indicate that :x should be between 10 and 20, use {:x {:$gt 10 :$lt 20}}.
    
    To indicate that :category should also be either :red, :green, or :blue, use :$in
    {:x {:$gt 10 :$lt 20} :y {:$in #{:green :blue :red}}}

    And to indicate that :category should not include :red, :green, or :blue, use :$nin
    {:x {:$gt 10 :$lt 20} :y {:$nin #{:green :blue :red}}}

    The available query terms include :$gt, :$lt, :$gte, :$lte, :$eq, :$ne, :$in, :$nin, $fn.

    A row predicate function can be used instead of a query-map. The function must accept 
    a map, representing a row of the dataset, and return a boolean value indicating whether 
    the row should be included in the new dataset.

   Examples:
      (use '(incanter core datasets))
      (def cars (get-dataset :cars))
      
      (view (query-dataset cars {:speed 10}))
      (view (query-dataset cars {:speed {:$in #{17 14 19}}}))
      (view (query-dataset cars {:speed {:$lt 20 :$gt 10}}))
      (view (query-dataset cars {:speed {:$fn #(> (log %) 3)}}))

      ;; use a row predicate function instead of a query map.
      (view (query-dataset cars (fn [row] (> (/ (row \"speed\") (row \"dist\")) 1/2))))

"
  ([data query-map]
     (if (fn? query-map)
       (assoc data :rows
              (for [row (:rows data) :when (query-map row)] row))
       (let [qmap (into {}
                        (for [k (keys query-map)] 
                          (if (keyword? k) 
                            (if (some #{k} (:column-names data))
                              [k (query-map k)]
                              [(name k) (query-map k)])
                            [k (query-map k)])))
             pred (query-to-pred qmap)
             rows (:rows data)]
         (assoc data :rows
                (for [row rows :when (pred row)] row))))))
Vars in incanter.core/query-dataset: assoc defn fn? for let
Used in 0 other vars

Comments top

No comments for query-dataset. Log in to add a comment.