I have to group by summarize some data in which first column contains years info. I am trying to do it, but getting errors.
Example data is:
mydata = data.frame(Year = c(2001:2018), Dat = c(1:18))
I want to mean aggregate means of "Dat" but group by "Year" divided into groups of 4 (i.e. 2001, 2002, 2003 and 2004 = Group 1 and so forth).
What I am trying:
ggplot(mydata, aes(x=group_by((n=n(Year)/4)), y=Dat)) + stat_summary(fun.y="mean", geom="bar")
But this is throwing error which I am not able to understand.
Error in n(Year) : unused argument (Year)
What I am doing wrong? or is there an alternative to it?