次のような data.frame があります。
> head(activity_data)
ev_id cust_id active previous_active start_date
1 1141880 201 1 0 2008-08-17
2 4927803 201 1 0 2013-03-17
3 1141880 244 1 0 2008-08-17
4 2391524 244 1 0 2011-02-05
5 1141868 325 1 0 2008-08-16
6 1141872 325 1 0 2008-08-16
cust_id ごとに
各 ev_id に対して
- 新しい変数 $recent_active を作成します (= $start_date > [this_row]$start_date - 10 の場合、この cust_id を持つすべての行で $active を合計します)
私の分割グループ化は .(cust_id) で、cust_id と ev_id で行を返したかったので、ddply を使用してこれを行うのに苦労しています。
これが私が試したものです
ddply(activity_data, .(cust_id), function(x) recent_active=sum(x[this_row,]$active))
ddply がオプションでない場合、他にどのような効率的な方法をお勧めしますか? 私のデータセットには約 2 億行あり、行ごとに約 10 ~ 15 回これを行う必要があります。
サンプルデータはこちら