[R] distributing a value for a given month across the number of weeks in that month

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri Jul 9 15:35:49 CEST 2010


Hello!

Any hint would be greatly appreciated.
I have a data frame that contains (a) monthly dates and (b) a value
that corresponds to each month - see the data frame "monthly" below:

monthly<-data.frame(month=c(20100301,20100401,20100501),monthly.value=c(100,200,300))
monthly$month<-as.character(monthly$month)
monthly$month<-as.Date(monthly$month,"%Y%m%d")
(monthly)

I need to split each month into weeks, e.g., weeks that start on
Monday (it could as well be Sunday - it does not really matter) and
distribute the monthly value evenly across weeks. So, if a month has 5
Mondays, then the monthly value should be dividied by 5, but if a
month has only 4 weeks, then the monthly value should be divided by 4.

The output I need is like this:

week          weekly.value
2010-03-01   20
2010-03-08   20
2010-03-15   20
2010-03-22   20
2010-03-29   20
2010-04-05   50
2010-04-12   50
2010-04-19   50
2010-04-26   50
2010-05-03   60
2010-05-10   60
2010-05-17   60
2010-05-24   60
2010-05-31   60

Thanks a lot for your advice!

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list