Here are two similar functions: column name and save order.
(defn transform-column [col-name f data] (let [new-col-names (sort-by #(= % col-name) (col-names data)) new-dataset (conj-cols (sel data :except-cols col-name) (f ($ col-name data)))] ($ (col-names data) (col-names new-dataset new-col-names) ))) (defn transform-rows [col-name f data] (let [new-col-names (sort-by #(= % col-name) (col-names data)) new-dataset (conj-cols (sel data :except-cols col-name) ($map f col-name data))]
And here is an example illustrating the difference:
=> (def test-data (to-dataset [{:a 1 :b 2} {:a 3 :b 4}])) => (transform-column :a (fn [x] (map
transform-rows best suited for simple transforms, where as transform-column is when the conversion for one row depends on other rows (for example, when a column is normalized).
Saving and loading CSV can be done with the standard Incanter functions, so the full example looks like this:
(use '(incanter core io))) (def data (col-names (read-dataset 'data.csv') [:a :b]) (save (transform-rows :a #(* % 2) data) 'transformed-data.csv')
Xavier shay
source share