我有20年的每月XTS时间序列
1990年1月12.3
1990年2月45.6
1990年3月78.9
..
1991年1月34.5
..
2009年12月89.0
我想获得平均(12个月)的年份,或者
1月xx
2月yy
...
Dec kk
其中xx是每年一月的平均值,每年二月的yy,依此类推。
我尝试了apply.yearly和lapply,但是它们返回1的值,这是20年的总平均值。
您有什么建议吗?我很感激。
答案 0 :(得分:0)
select filter_type
from filters
group by filter_type
having count(*) > 1 and
min(name) = max(name)
软件包可能对您有用。我将lubridate
和year()
函数与month()
结合使用:
aggregate()
输出类似:
library(xts)
library(lubridate)
#set up some sample data
dates = seq(as.Date('2000/01/01'), as.Date('2005/01/01'), by="month")
df = data.frame(rand1 = runif(length(dates)), rand2 = runif(length(dates)))
my_xts = xts(df, dates)
#get the mean by year
aggregate(my_xts$rand1, by=year(index(my_xts)), FUN=mean)
要查找每个月的平均值,您可以执行以下操作:
2000 0.5947939
2001 0.4968154
2002 0.4941752
2003 0.5291211
2004 0.6631564
将输出类似
的内容#get the mean by month
aggregate(my_xts$rand1, by=month(index(my_xts)), FUN=mean)