Created
October 29, 2012 02:59
-
-
Save guilespi/3971236 to your computer and use it in GitHub Desktop.
Iterating over a dataset to apply specific functions to each row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro apply-filtered | |
"Given two sequences, apply a function to each pair of elements when condition is met | |
anaphoras n and m exists for each indexed element | |
e.g. (apply-filtered / [1 2 3] [1 0 3] when (> m 0)) => (1 nil 1) | |
" | |
[op a b & condition] | |
`(for [x# (range (count ~a))] | |
(let [n# (nth ~a x#) | |
m# (nth ~b x#)] | |
(when (~(second condition) n# m#) | |
(~op n# m#))))) | |
(defmacro apply-rows | |
"Apply an operation to each row of the dataset excluding :Date column | |
A start row and a condition must be given | |
.e.g Divide each row by vector [1 0 3] starting from 0 validating divide by zero | |
(apply-rows ds (/ [1 2 3]) 0 (fn [n m] (> m 0))" | |
[data operation start cond] | |
`(let [raw-data# (incanter.core/$ :all [:not :Date] ~data) | |
raw-cols# (incanter.core/col-names raw-data#) | |
dates# (incanter.core/$ :all :Date ~data)] | |
(incanter.core/col-names | |
(incanter.core/conj-cols | |
(for [~'i (range ~start (incanter.core/nrow raw-data#))] | |
(apply-filtered | |
~(first operation) | |
(vec (incanter.core/$ ~'i [:not :Date] raw-data#)) | |
~(second operation) | |
:when ~cond)) | |
dates#) (conj raw-cols# :Date)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment