First create a week
variable using format
. Then use ddply
and transform
.
library(plyr)
data.df$week <- format(data.df$date,'%W %Y') #week begins with Monday
data.df <- ddply(data.df,~week,transform,percent=y/mean(y))
head(data.df)
y date week percent
1 1.2629543 2008-01-01 00 2008 3.1395415
2 -0.3262334 2008-01-02 00 2008 -0.8109741
3 1.3297993 2008-01-03 00 2008 3.3057095
4 1.2724293 2008-01-04 00 2008 3.1630952
5 0.4146414 2008-01-05 00 2008 1.0307451
6 -1.5399500 2008-01-06 00 2008 -3.8281172
Note that week 00 usually is not a full week as is the last week of the year. Merge last and first weeks of subsequent years if that matters.