1.2.3-SNAPSHOT permalink Arrow_down_16x16

to-dataset

incanter.core

  • (to-dataset obj & options)
Returns a dataset containing the given values.

Examples:

(use 'incanter.core)
(to-dataset 1)
(to-dataset :a)
(to-dataset [:a])
(to-dataset (range 10))
(to-dataset (range 10) :transpose true)
(to-dataset [[1 2] [3 4] [5 6]])
(to-dataset {:a 1 :b 2 :c 3})
(to-dataset {"a" 1 "b" 2 "c" 3})
(to-dataset [{:a 1 :b 2} {:a 1 :b 2}])
(to-dataset [{"a" 1 "b" 2 "c" 3} {"a" 1 "b" 2 "c" 3}])

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:1359 top

(defn to-dataset
  "Returns a dataset containing the given values.

   Examples:

     (use 'incanter.core)
     (to-dataset 1)
     (to-dataset :a)
     (to-dataset [:a])
     (to-dataset (range 10))
     (to-dataset (range 10) :transpose true)
     (to-dataset [[1 2] [3 4] [5 6]])
     (to-dataset {:a 1 :b 2 :c 3})
     (to-dataset {\"a\" 1 \"b\" 2 \"c\" 3})
     (to-dataset [{:a 1 :b 2} {:a 1 :b 2}])
     (to-dataset [{\"a\" 1 \"b\" 2 \"c\" 3} {\"a\" 1 \"b\" 2 \"c\" 3}])

"
  ([obj & options]
     (let [opts (when options (apply assoc {} options))
	   transpose? (true? (:transpose opts))
	   colnames (cond
		     (dataset? obj)
		       (:column-names obj)
		     (map? obj)
		       (keys obj)
		     (coll? obj)
		       (cond 
			(map? (first obj))
			  (keys (first obj))
			(coll? (first obj))
			  (map #(keyword (str "col-" %)) (range (length (first obj))))
			transpose?
			  (map #(keyword (str "col-" %)) (range (length obj)))
			:else
			  [:col-0])
		     :else
		       [:col-0]) 
	   rows (cond
		 (dataset? obj)
		   (:rows obj)
		 (map? obj)
		   ;; see if any of the values are collections
		   (if (reduce #(or %1 %2) (map coll? (vals obj))) 
		     (vals obj)
		     [(vals obj)])
		   (coll? obj)
		     (cond
		       (coll? (first obj)) 
		         obj
		       transpose?
		         [obj]
		       :else
		         obj)
		   :else
		     [obj])]
         (dataset colnames rows))))
Vars in incanter.core/to-dataset: defn let
Used in 0 other vars

Comments top

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