I have the following table:
Date Country Class Value
6/1/2010 USA A 45
6/1/2010 Canada A 23
6/1/2010 Brazil B 65
9/1/2010 USA B 47
9/1/2010 Canada A 98
9/1/2010 Brazil B 25
12/1/2010 USA B 14
12/1/2010 Canada A 79
12/1/2010 Brazil A 23
3/1/2011 USA A 84
3/1/2011 Canada B 77
3/1/2011 Brazil A 43
6/1/2011 USA A 45
6/1/2011 Canada A 23
6/1/2011 Brazil B 65
9/1/2011 USA B 47
9/1/2011 Canada A 98
9/1/2011 Brazil B 25
12/1/2011 USA B 14
12/1/2011 Canada A 79
12/1/2011 Brazil A 23
3/1/2012 USA A 84
3/1/2012 Canada B 77
3/1/2012 Brazil A 43
In column "Date" years are divided by the following months - March, June, September and December. I need to group months from June to March as a Fiscal Year and by each Fiscal Year calculate the mean of column "Value" by "Country" and "Class". Could anybody help me to do that?
I am trying to do that using data.table but get the error:
d=data[,list(Val=mean(Value,na.rm=T)),by=list(Country,Class,
Period.grp=cut(Period,list(6/1/2010,3/1/2011,6/1/2011,3/1/2012,
6/1/2012,3/1/2013,6/1/2013,3/1/2014)))]
Error in cut.default(Period, list(6/1/2010, 3/1/2011, 6/1/2011, 3/1/2012, : 'x' must be numeric
Thank you!
I found the answer I was thinking I had written, but it's actually a bit different
This then pastes the FY with the quarter shifted 6 months, but you may need to adjust because your year specification was ambiguous about "naming the year":
So now do a tapply or aggregate by FY_Q and Country:
See: Format date-time as seasons in R? for a somewhat similar problem and solutions.
Try:
For FY2011 etc: