Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Date Value
- 2012-05-28 101
- 2012-05-25 99
- 2012-05-24 102
- ....
- 2012-04-30 78
- 2012-04-27 82
- 2012-04-26 77
- 2012-04-25 75
- 2012-04-24 76
- 1. 2012-05-28 to 2012-04-30
- 2. 2012-05-25 to 2012-04-26
- 3. 2012-05-24 to 2012-04-25
- #function for extracting month is in the lubridate package
- install.packages(c("plyr", "lubridate"))
- require(plyr); require(lubridate)
- #read the daily data
- daily = read.csv("daily_lumber_prices.csv")
- price = daily$Open
- date = daily$Date
- #convert date to a usable format
- date = strptime(date, "%d-%b-%y")
- mon = month(date)
- T = length(price)
- #need to know when months change
- change_month = rep(0,T)
- for(t in 2:T){
- if(mon[t] != mon[t-1]){
- change_month[t-1] = 1
- }
- }
- month_avg = rep(0,T)
- total = 0
- days = 0
- for(t in 1:T){
- if(change_month[t] == 0){
- #cumulative sums for each variable
- total = total + price[t]
- days = days + 1
- }
- else{
- #need to include the current month in the calculation
- month_avg[t] = (total + price[t]) / (days + 1)
- #reset the variables
- total = 0
- days = 0
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement