fslaborg / Deedle

Easy to use .NET library for data and time series manipulation and for scientific programming
http://fslab.org/Deedle/
BSD 2-Clause "Simplified" License
935 stars 195 forks source link

Add "sweep level" function #236

Open tpetricek opened 10 years ago

tpetricek commented 10 years ago

Include something like this (inspired by R "sweep" operation):

let sweepLevel level summaryOp sweepOp series =
    let summary = series |> Series.applyLevel level summaryOp
    let sumValues = summary |> Series.getAll (series.Keys |> Seq.map level) |> Series.values
    let sumSeries = Series(series.Keys, sumValues)
    Series.zipInto sweepOp series sumSeries

So, for example, to normalize each values (divide by the sum), we can write:

f?N |> sweepLevel fst Stats.sum (/)

(I think the level aggregation functions have a bit confusing naming, so adding this contributes to that, but it is a super useful function... Perhaps there is some better naming scheme we could follow!)

johannh-zz commented 10 years ago

After I wrote that, I swapped the order of the summaryOp and sweepOp parameters to better reflect the order in which they're applied in the zipInto call, e.g.

let sweepLevel level sweepOp summaryOp series = ...

and

f?N |> sweepLevel fst (/) Stats.sum

I definitely like it, but I agree that it adds to the naming confusion.