[英]sum/aggregate data based on dates, R
I have a data set like following: 我有一个如下数据集:
Date Country Item Qty Value
15-04-2014 SE 08888 2 20
28-04-2014 SE 08888 2 20
05-05-2014 SE 08888 6 80
I want to sum quantity values when the dates are before the 1 May, and the aggregated value (the sum) should be marked as 1 May. 我想在日期为5月1日之前对数量值求和,并且合计值(总和)应标记为5月1日。
I tried ddply
, but it sums all the value regardless of the dates. 我尝试了ddply
,但是无论日期如何,它都会对所有值求和。
ddply(se, .(se$Item), summarize, Qty = sum(se$Qty), Value = sum(se$Value))
Also tried subsetting by the date, with no big success. 还尝试按日期进行子集化,但没有大的成功。
se$Date <- as.Date(as.character(se$Date))
se_q <- subset(se,se$Date <= 01-05-2014)
Date Country Item Qty Value
0015-04-20 SE 08888 2 20
0028-04-20 SE 08888 2 20
0005-05-20 SE 08888 6 80
How could I add the date argument in the code? 如何在代码中添加date参数? or how could I do this? 或我该怎么做?
Thank you 谢谢
You could use dplyr
for example: 您可以使用dplyr
例如:
require(dplyr)
> df %.%
filter(Date <= as.Date("2014-05-01")) %.%
# group_by(Item) %.% #you can add this line if you need to group by Item (it will appear in the output then)
summarize(Date = as.Date("2014-05-01"), Qty = sum(Qty), Value = sum(Value))
# Date Qty Value
#1 2014-05-01 4 40
The problem in your subset
is that you are not telling R that 2014-05-01
is a Date
. 您的subset
的问题是您没有告诉R 2014-05-01
是一个Date
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.