Possible Duplicate:
The hourly mean in time series可能重复:时间序列中的每小时平均值
I have a time series:
我有一个时间序列:
It is data that is measured every 30min, so I have 536 days with
这是每30分钟测量一次的数据,所以我有536天
n=25728.
It is a ts
with one column and freq=48
. I would like to calculate the mean for every 30min.
它是一列有一列,freq = 48。我想计算每30分钟的平均值。
So for example:
例如:
First 30min:
ts[1]+t[49]+t[97]+t[145]+.../536 days
Second 30min:
t[2]+t[50]+t[98]+..../536 days
What is the best code?
什么是最好的代码?
1 个解决方案
#1
1
To achieve this effect you can use aggregate
or tapply
, see also:
要实现此效果,您可以使用aggregate或tapply,另请参阅:
The hourly mean in time series
时间序列中的每小时平均值
Calculate monthly average of ts object
计算ts对象的月平均值
Some example code:
一些示例代码:
time_series = runif(72)
# Create a ts object
time_ts = ts(time_series, frequency = 24)
# Calculate the mean
> tapply(time_ts, cycle(time_ts), mean)
1 2 3 4 5 6 7 8
0.2954238 0.6791355 0.6113670 0.5775792 0.3614329 0.4414882 0.6206761 0.2079882
9 10 11 12 13 14 15 16
0.6238492 0.4069143 0.6333607 0.5254185 0.6685191 0.3629751 0.3715500 0.2637383
17 18 19 20 21 22 23 24
0.2730713 0.3170541 0.6053016 0.6550780 0.4031117 0.6857810 0.4492246 0.4795785
> aggregate(as.numeric(time_ts), list(hour = cycle(time_ts)), mean)
hour x
1 1 0.2954238
2 2 0.6791355
3 3 0.6113670
4 4 0.5775792
#1
1
To achieve this effect you can use aggregate
or tapply
, see also:
要实现此效果,您可以使用aggregate或tapply,另请参阅:
The hourly mean in time series
时间序列中的每小时平均值
Calculate monthly average of ts object
计算ts对象的月平均值
Some example code:
一些示例代码:
time_series = runif(72)
# Create a ts object
time_ts = ts(time_series, frequency = 24)
# Calculate the mean
> tapply(time_ts, cycle(time_ts), mean)
1 2 3 4 5 6 7 8
0.2954238 0.6791355 0.6113670 0.5775792 0.3614329 0.4414882 0.6206761 0.2079882
9 10 11 12 13 14 15 16
0.6238492 0.4069143 0.6333607 0.5254185 0.6685191 0.3629751 0.3715500 0.2637383
17 18 19 20 21 22 23 24
0.2730713 0.3170541 0.6053016 0.6550780 0.4031117 0.6857810 0.4492246 0.4795785
> aggregate(as.numeric(time_ts), list(hour = cycle(time_ts)), mean)
hour x
1 1 0.2954238
2 2 0.6791355
3 3 0.6113670
4 4 0.5775792