次のデータがあります。
let data = [(41609.00 , 10000., 3.822); (41609.00, 60000., 3.857); (41974.00 , 20000., 4.723 ); (41974.00, 30000., 3.22 ); (41974.00 , 4000., 4.655 ); (42339.00, 7000., 4.22 ); (42339.00 , 5000., 3.33)]
最初の列 = OADate、2 番目 = 出来高、3 番目 = 価格。
日付ごとにグループ化し、ボリュームを合計して、加重平均価格を計算したいと思います。これは私がこれまでに持っているものです:
let aggr data =
data
//Multiply second and third column element by element
|> Seq.map (fun (a, b, c) -> (a, b, b * c))
//Group by first column
|> Seq.groupBy fst
//Sum column 2 & 3 based on group of column 1
|> Seq.map (fun (d, e, f) -> (d, e |> Seq.sum, f |> Seq.sum))
//take the sum and grouped column 1 & 2 and compute weighted average of the third
|> Seq.map (fun (g, h, i) -> (g, h, i/h))
タプルの長さが異なるというタイプの不一致が発生しています。以前も同様の構文を問題なく使用していました。誰かが私を正しい方向に向けてもらえますか?
アップデート:
誰かが興味を持っている場合の解決策は次のとおりです。トーマスとリーフに感謝します
let aggr data =
data
|> Seq.map (fun (a, b, c) -> (a, b, b * c))
|> Seq.groupBy (fun (a, b, c) -> a)
|> Seq.map (fun (key, group) -> group |> Seq.reduce (fun (a, b, c) (x, y, z) -> a, b+y , c+z))
|> Seq.map (fun (g, h, i) -> (g, h, i/h))