Amount / aggregate data based on dates, R

I have a data set, for example:

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 summarize the quantitative values ​​when the dates are before May 1, and the aggregated value (amount) should be marked as May 1.

I tried ddply , but it sums all the values ​​regardless of dates.

 ddply(se, .(se$Item), summarize, Qty = sum(se$Qty), Value = sum(se$Value)) 

Also tried a subset by date, without much 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 to add a date argument to code? or how can i do this?

thanks

0
date r
source share
1 answer

You can use dplyr , for example:

 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 with your subset is that you are not 2014-05-01 R that 2014-05-01 is Date .

0
source share

All Articles