2015-12-02 97 views
1

我有一個數據幀,看起來像這樣:rollsum與固定日期

user_id  date    price 
2375 2012/12/12 00:00:00.000 47.900000 
2375 2013/01/16 00:00:00.000 47.900000 
2375 2013/01/16 00:00:00.000 47.900000 
2375 2013/05/08 00:00:00.000 47.900000 
2375 2013/06/01 00:00:00.000 47.900000 
2375 2013/10/02 00:00:00.000 26.500000 
2375 2014/01/22 00:00:00.000 47.900000 
2375 2014/03/21 00:00:00.000 47.900000 
2375 2014/05/24 00:00:00.000 47.900000 
2375 2015/04/11 00:00:00.000 47.900000 
7419 2012/12/12 00:00:00.000 7.174977 
7419 2013/01/02 00:00:00.000 27.500000 
7419 2013/01/18 00:00:00.000 22.901482 
7419 2013/02/08 00:00:00.000 27.500000 
7419 2013/03/06 00:00:00.000 8.200000 
7419 2013/04/03 00:00:00.000 22.901482 
7419 2013/04/03 00:00:00.000 8.200000 
7419 2013/04/03 00:00:00.000 6.900000 
7419 2013/04/17 00:00:00.000 7.500000 
7419 2013/04/17 00:00:00.000 7.500000 
7419 2013/05/23 00:00:00.000 7.500000 
7419 2013/06/07 00:00:00.000 27.500000 
7419 2013/06/07 00:00:00.000 7.500000 
7419 2013/06/07 00:00:00.000 7.500000 
7419 2013/06/07 00:00:00.000 5.829188 
7419 2013/07/10 00:00:00.000 27.500000 
7419 2013/08/21 00:00:00.000 7.500000 
7419 2013/08/21 00:00:00.000 27.500000 
7419 2013/09/06 00:00:00.000 27.500000 
7419 2013/12/27 00:00:00.000 7.500000 
7419 2014/01/10 00:00:00.000 27.500000 
7419 2014/02/16 00:00:00.000 27.500000 
7419 2014/05/14 00:00:00.000 41.900000 
7419 2014/07/03 00:00:00.000 26.500000 
7419 2014/09/26 00:00:00.000 26.500000 
7419 2014/09/26 00:00:00.000 7.500000 
7419 2014/10/22 00:00:00.000 27.500000 
7419 2014/11/15 00:00:00.000 6.900000 
7419 2014/11/27 00:00:00.000 26.500000 
7419 2014/12/12 00:00:00.000 40.900000 
7419 2015/01/14 00:00:00.000 27.200000 
7419 2015/02/24 00:00:00.000 26.500000 
7419 2015/03/17 00:00:00.000 40.900000 
7419 2015/05/02 00:00:00.000 27.200000 
7419 2015/05/02 00:00:00.000 26.500000 
7419 2015/05/15 00:00:00.000 7.900000 
7419 2015/05/20 00:00:00.000 27.500000 
7419 2015/06/20 00:00:00.000 7.500000 
7419 2015/06/26 00:00:00.000 7.500000 
7419 2015/06/30 00:00:00.000 41.900000 
7419 2015/07/16 00:00:00.000 78.500000 
11860 2012/12/12 00:00:00.000 7.174977 
11860 2012/12/12 00:00:00.000 21.500000 
11860 2013/03/02 00:00:00.000 22.901482 
11860 2013/03/02 00:00:00.000 8.200000 
11860 2013/05/25 00:00:00.000 29.500000 
11860 2013/05/25 00:00:00.000 7.500000 

在現實中,我有超過40000 USER_ID。我想計算每個用戶的價格前4周(不包括本週)的總和。但是,日期從2012年12月12日至2015年9月22日是固定的。爲了避免爲每個用戶一個循環,我想到了什麼樣

df <- df %>% group_by(user_id) %>% 
    mutate(price.lag1 = lag(prod_price, n = 1)) %>% 
    mutate(amount4weeks = rollsum(x=price, 4, align = "right", fill = NA)) 

但是,它給了我一個錯誤,而且只需要爲「日期」存在於數據的行。

我該如何給出特定的日期和/或如何在一行內完成我想要的工作?我的結果應該是這樣的:

df$price4weeks = c(NA, 0.000000, 0.000000, 0.000000, 47.900000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, NA, 7.174977, 27.500000, 22.901482, 27.500000, 8.200000, 8.200000, 8.200000, 6.900000, 6.900000, 0.000000, 7.500000, 7.500000, 7.500000, 7.500000, 0.000000, 0.000000, 0.000000, 27.500000, 0.000000, 7.500000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 7.500000, 27.500000, 6.900000, 33.400000, 0.000000, 0.000000, 26.500000, 0.000000, 0.000000, 26.500000, 34.400000, 27.500000, 7.500000,15.000000, 56.900000, NA, NA, 0.000000, 0.000000, 0.000000, 0.000000) 

讓我知道如果我在我的解釋中遺漏了某些東西。

謝謝!

回答

2

rollsum計算滾動k個數據點的總和。要在幾周內使用dplyr,您可以將week_number列添加到您的數據中,然後使用sapply而不是week_number來計算滾動總和。該代碼可能看起來像:

df <- mutate(df, week_number=cut.POSIXt(df$date, breaks="week", labels=FALSE)) 
df_new <- df %>% group_by(user_id) %>% 
     do(mutate(.,total_4wk=sapply(week_number, function(n) sum(.$price[between(.$week_number, n -4, n-1)],na.rm=TRUE)))) 
+0

這是行得通!謝謝! – Andres

+0

唯一讓我擔心的是,在我的預期結果中,當k窗口內沒有數據點時,我得到了NA,而用公式得到0,我會看看這個。再次感謝! – Andres