[英]R, time series, average, sequence
Hey guys I have a zoo object of daily observations for 30 years that Looks something like this: 嘿,我有一个动物园对象,每天观察30年,看起来像这样:
date x
1971-11-01 145.234
1971-11-02 234.522
1971-11-03 423.32
1971-11-04 333.11
I would like to calculate means for the period November to April for the entire time series. 我想计算整个时间序列在11月到4月期间的均值。 So my desired result should look like this: 所以我想要的结果应该是这样的:
date x
11/1971-04/1972 642.43
11/1972-04/1973 142.53
11/1973-04/1974 642.39
11/1974-04/1975 424.75
11/1975-04/1976 185.34
Can somebody help me? 有人可以帮我吗?
If your dates are truly daily such that November first appears every year, the following strategy will allow you to group the observations. 如果您的约会确实是每天的,以至于每年的11月首次出现,则可以使用以下策略对观察进行分组。
# drop observations outside of the window (May through October).
dfNew <- df[!grepl("^(0[56789]|10)", format(df$date, "%m")), ]
# build groups
dfNew$groups <- cumsum(c(TRUE, tail(grepl("11-01", format(dfNew$date, "%m-%d")), -1)))
The first line uses a logical vector based on a regular expression to drop months May through October. 第一行使用基于正则表达式的逻辑向量将5月份减少到10月份。 The second line uses a regular expression to return a logical vector indicating whether the observation is November first. 第二行使用正则表达式返回逻辑向量,该逻辑向量指示观察结果是否为十一月第一。 I cut off the first element of the vector using tail
and added TRUE, to make sure the that the group count begins with 1. cumsum
is then used to create the group indicators. 我使用tail
切断向量的第一个元素,并添加TRUE,以确保组计数以1开头。然后使用cumsum
创建组指示符。
At this point, you can use aggregate
, for example, to get the group mean: 此时,例如,您可以使用aggregate
获取组均值:
aggregate(x ~ groups, data=dfNew, FUN=mean)
groups x
1 1 -0.14947871
2 2 -0.02742739
3 3 -0.02296979
4 4 0.01939372
5 5 -0.01432937
6 6 0.10393297
7 7 0.06660049
8 8 -0.03955617
9 9 -0.06956639
data 数据
set.seed(1234)
df <- data.frame(date=seq(as.Date("1971-01-01"), as.Date("1979-01-01"), by="day"),
x=rnorm(2923))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.