我有一个日期列和价格列:
date = c(2012/12/12 00:00:00.000, 2012/12/12 00:00:00.000, 2012/12/12 00:00:00.000, 2012/12/19 00:00:00.000, 2012/12/19 00:00:00.000, 2013/01/02 00:00:00.000, 2013/01/09 00:00:00.000, 2013/01/19 00:00:00.000, 2013/02/05 00:00:00.000, 2013/02/19 00:00:00.000, 2013/02/26 00:00:00.000, 2013/03/05 00:00:00.000, 2013/03/12 00:00:00.000, 2013/03/20 00:00:00.000, 2013/03/26 00:00:00.000, 2013/04/02 00:00:00.000, 2013/04/09 00:00:00.000, 2013/04/16 00:00:00.000, 2013/04/23 00:00:00.000, 2013/04/30 00:00:00.000, 2013/05/08 00:00:00.000, 2013/06/04 00:00:00.000, 2013/06/29 00:00:00.000, 2013/07/29 00:00:00.000, 2013/08/26 00:00:00.000, 2013/09/19 00:00:00.000, 2013/10/14 00:00:00.000, 2013/10/21 00:00:00.000, 2013/10/30 00:00:00.000, 2013/11/05 00:00:00.000, 2013/11/13 00:00:00.000, 2013/11/20 00:00:00.000, 2013/11/26 00:00:00.000, 2013/12/03 00:00:00.000, 2013/12/10 00:00:00.000, 2013/12/19 00:00:00.000, 2013/12/26 00:00:00.000, 2014/01/03 00:00:00.000, 2014/01/09 00:00:00.000, 2014/01/24 00:00:00.000, 2014/01/31 00:00:00.000, 2014/02/06 00:00:00.000, 2014/02/13 00:00:00.000, 2014/02/20 00:00:00.000, 2014/03/01 00:00:00.000, 2014/03/08 00:00:00.000, 2014/03/14 00:00:00.000, 2014/03/28 00:00:00.000, 2014/04/03 00:00:00.000, 2014/04/10 00:00:00.000, 2014/04/25 00:00:00.000, 2014/05/02 00:00:00.000, 2014/05/09 00:00:00.000, 2014/06/28 00:00:00.000, 2014/07/05 00:00:00.000, 2014/07/23 00:00:00.000)
price = rep(23.5, 56)
每个日期都是交易日期,价格只是产品的价格。我想计算过去12周的花费金额。这意味着每次有交易日期,我都会向后计算12周。我可以计算累积金额:
df = data.table(日期=日期,价格=价格) df [,quantity.csum:= cumsum(price)]
我觉得这很简单,但我找不到办法在12周内(或者我以后决定的任何一周)。
有人能帮助我吗?
谢谢!
答案 0 :(得分:0)
定义一个“期间”列,用于定义您要考虑和使用的时间段,例如aggregate
date <- c("2012/12/12 00:00:00.000", "2012/12/12 00:00:00.000",
"2012/12/12 00:00:00.000", "2012/12/19 00:00:00.000", "2012/12/19 00:00:00.000",
"2013/01/02 00:00:00.000", "2013/01/09 00:00:00.000", "2013/01/19 00:00:00.000",
"2013/02/05 00:00:00.000", "2013/02/19 00:00:00.000", "2013/02/26 00:00:00.000",
"2013/03/05 00:00:00.000", "2013/03/12 00:00:00.000", "2013/03/20 00:00:00.000")
price <- rep(23.5, 14)
period <- c(rep("period1",5), rep(c("period2", "period3", "period4"), each=3))
df <- data.frame(date=date, price=price, period=period)
df
date price period
1 2012/12/12 00:00:00.000 23.5 period1
2 2012/12/12 00:00:00.000 23.5 period1
3 2012/12/12 00:00:00.000 23.5 period1
4 2012/12/19 00:00:00.000 23.5 period1
5 2012/12/19 00:00:00.000 23.5 period1
6 2013/01/02 00:00:00.000 23.5 period2
7 2013/01/09 00:00:00.000 23.5 period2
8 2013/01/19 00:00:00.000 23.5 period2
9 2013/02/05 00:00:00.000 23.5 period3
10 2013/02/19 00:00:00.000 23.5 period3
11 2013/02/26 00:00:00.000 23.5 period3
12 2013/03/05 00:00:00.000 23.5 period4
13 2013/03/12 00:00:00.000 23.5 period4
14 2013/03/20 00:00:00.000 23.5 period4
aggregate(price~period, data=df, sum)
period price
1 period1 117.5
2 period2 70.5
3 period3 70.5
4 period4 70.5
编辑:
df <- data.frame(date=date, price=price)
df$date1 <- as.Date(df$date)
df[df$date1 >= "2012-12-12" & df$date1 <= "2012-12-19", "period"] <- "period 1"
df[df$date1 >= "2013-01-02" & df$date1 <= "2013-02-19", "period"] <- "period 2"
df[df$date1 >= "2013-02-26" & df$date1 <= "2013-03-20", "period"] <- "period 3"
aggregate(price~period, data=df, sum)
编辑2:
twelve_weeks <- 7*12 # Seven days in one week
df$date1 - twelve_weeks # Vector containing dates twelve
# weeks before each transaction dates