Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. Date Value
  2. 2012-05-28 101
  3. 2012-05-25 99
  4. 2012-05-24 102
  5. ....
  6. 2012-04-30 78
  7. 2012-04-27 82
  8. 2012-04-26 77
  9. 2012-04-25 75
  10. 2012-04-24 76
  11.  
  12. 1. 2012-05-28 to 2012-04-30
  13. 2. 2012-05-25 to 2012-04-26
  14. 3. 2012-05-24 to 2012-04-25
  15.  
  16. #function for extracting month is in the lubridate package
  17. install.packages(c("plyr", "lubridate"))
  18. require(plyr); require(lubridate)
  19.  
  20. #read the daily data
  21. daily = read.csv("daily_lumber_prices.csv")
  22. price = daily$Open
  23. date = daily$Date
  24.  
  25. #convert date to a usable format
  26. date = strptime(date, "%d-%b-%y")
  27. mon = month(date)
  28. T = length(price)
  29.  
  30. #need to know when months change
  31. change_month = rep(0,T)
  32.  
  33. for(t in 2:T){
  34. if(mon[t] != mon[t-1]){
  35. change_month[t-1] = 1
  36. }
  37. }
  38.  
  39. month_avg = rep(0,T)
  40. total = 0
  41. days = 0
  42.  
  43. for(t in 1:T){
  44. if(change_month[t] == 0){
  45. #cumulative sums for each variable
  46. total = total + price[t]
  47. days = days + 1
  48. }
  49.  
  50. else{
  51. #need to include the current month in the calculation
  52. month_avg[t] = (total + price[t]) / (days + 1)
  53. #reset the variables
  54. total = 0
  55. days = 0
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement