Reputation: 7895
I have some daily time-series data that i need to extract the 'week day percent' relative to the week mean. For example, if the first week has mean = 100
and the Sunday value for this week is 20, then sunday becomes 0.2.
Here's some random data:
set.seed(0)
y = rnorm(1705)
date = seq(as.Date('2008-01-01'), by = 'day', length = length(y))
data.df = data.frame(y, date)
I need a new column called pecent
, which is the value explained above. I tried to add some
columns then use tapply
, but failed. Appreciate any help!
Upvotes: 0
Views: 127
Reputation: 132706
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.
Upvotes: 3