0

次のような 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 回これを行う必要があります。

サンプルデータはこちら

4

1 に答える 1

0

ここでは実際に 2 段階のアプローチを使用する必要があります (また、次のコードを使用する前に日付を日付形式に変換する必要があります)。

ddply(activity_date, .(cust_id), transform, recent_active=your function) #Not clear what you are asking regarding the function

ddply(activity_date, .(cust_id,ev_id), summarize,recent_active=sum(recent_active))
于 2013-08-22T14:26:29.863 に答える