Skip to content

Commit

Permalink
Merge branch 'master' of github.com:liebke/incanter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexott committed Sep 1, 2013
2 parents d9dbe9a + e9a0ce3 commit 0df9db7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
52 changes: 52 additions & 0 deletions modules/incanter-core/src/incanter/core.clj
Expand Up @@ -2027,6 +2027,58 @@ altering later ones."
replace-by-number-or-value old-col-names col-map)]
(col-names data new-col-names))))

(defn- update
([m key f] (update-in m [key] f))
([m key f & kfs] (apply update (update-in m [key] f) kfs)))

(defn replace-column
"Replaces a column in a dataset with new values."
([column-name values]
(replace-column column-name values $data))
([column-name values data]
(update data :rows
(fn [rows]
(map #(assoc %1 column-name %2)
rows values)))))

(defn add-column
"Adds a column, with given values, to a dataset."
([column-name values]
(add-column column-name values $data))
([column-name values data]
(if (some #{column-name} (:column-names data))
(replace-column column-name values data)
(update data :column-names #(conj % column-name)
:rows #(mapv (fn [r v]
(assoc r column-name v))
% (concat values (repeat nil)))))))

(defn add-derived-column
"This function adds a column to a dataset that is a function of
existing columns. If no dataset is provided, $data (bound by the
with-data macro) will be used. f should be a function of the
from-columns, with arguments in that order.
Examples:
(use '(incanter core datasets))
(def cars (get-dataset :cars))
(add-derived-column :dist-over-speed [:dist :speed] (fn [d s] (/ d s)) cars)
(with-data (get-dataset :cars)
(view (add-derived-column :speed**-1 [:speed] #(/ 1.0 %))))"

([column-name from-columns f]
(add-derived-column column-name from-columns f $data))
([column-name from-columns f data]
(update data :column-names #(conj % column-name)
:rows (fn [rows]
(mapv (fn [row]
(assoc row column-name
(apply f (map #(map-get row %)
from-columns))))
rows)))))

;; credit to M.Brandmeyer
(defn transform-col
" Apply function f & args to the specified column of dataset and replace the column
Expand Down
10 changes: 10 additions & 0 deletions modules/incanter-core/test/incanter/dataset_tests.clj
Expand Up @@ -51,6 +51,16 @@
(is (= (head 2 dataset1) ($ (range 2) :all dataset1)))
(is (= (head dataset1) ($ (range 2) :all dataset1))))

(deftest test-add-column
(is (= (add-column :price [17599 22099] cars)
(dataset [:speed :weight :colour :price] [(conj car0 17599) (conj car1 22099)])))
(is (= (add-column :weight [2500 3500] cars)
(dataset [:speed :weight :colour] [(assoc car0 1 2500) (assoc car1 1 3500)]))))

(deftest test-add-derived-column
(is (= ($ :weight-minus-speed (add-derived-column :weight-minus-speed [:weight :speed] #(- %1 %2) cars))
[5940 6930])))

;; (deftest selects-on-badly-named-atoms
;; (let [with-nots (dataset [:first :second] [[:not :all] [:all :not]])]
;; (is (= ($ :first
Expand Down

0 comments on commit 0df9db7

Please sign in to comment.