我使用此代码根据每日价格数据计算每月平均值。
#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
}
}
因此,变量 month_avg 存储了每月平均值。
是这样的吗?此代码说明了月份的可变长度。当然有一种更有效的方法来做到这一点,但这很有效!