我有以下格式的一些数据:提取一个月的时间序列意味着R中
date x
2001/06 9949
2001/07 8554
2001/08 6954
2001/09 7568
2001/10 11238
2001/11 11969
... more rows
我想提取×平均每月。我尝试了一些代码,但 失败。感谢您为此提供的任何帮助。
我有以下格式的一些数据:提取一个月的时间序列意味着R中
date x
2001/06 9949
2001/07 8554
2001/08 6954
2001/09 7568
2001/10 11238
2001/11 11969
... more rows
我想提取×平均每月。我尝试了一些代码,但 失败。感谢您为此提供的任何帮助。
这里我模拟叫df
更多的数据的数据帧:
df <- data.frame(
date = apply(expand.grid(2001:2012,1:12),1,paste,collapse="/"),
x = rnorm(12^2,1000,1000),
stringsAsFactors=FALSE)
通过移除杉杉四个数字后跟一个斜杠使用您的date
载体构建,你可以得到个月的方式。在这里,我用这个作为索引变量tapply
计算方法:
with(df, tapply(x, gsub("\\d{4}/","",date), mean))
对不起...只是创建一个月份序列矢量然后使用tapply。 这是非常容易的:
m.seq = rep(c(6:12, 1:5), length = nrow(data))
m.means = tapply(data$x, m.seq, mean)
但反正评论的感谢!
看起来你已经有了它... – GSee 2012-08-16 13:01:05
无数据接着说:2002/01 ... 2012/01,2012/02。 – Fernando 2012-08-16 13:03:14