计算 Zoo 对象的每小时平均值
Calculating Average per hour for Zoo object
我有以下内容:
Lines <- "D1,Diff
1,20/11/2014 16:00,0.01
2,20/11/2014 17:00,0.02
3,20/11/2014 18:00,0.03
4,21/11/2014 16:00,0.04
5,21/11/2014 17:00,0.06
6,21/11/2014 18:00,0.07"
z <- read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")
ep <- endpoints(z,'hours')
a<-period.apply(z,ep,mean)
我想创建一个新的 Zoo 对象,每个小时都会有同一小时的 Diff 平均值。所需的结果将是:
16:00,0.025
17:00,0.03
18:00,0.05
内容一:
> a
2014-11-20 16:00:00 2014-11-20 17:00:00 2014-11-20 18:00:00 2014-11-21 16:00:00 2014-11-21 17:00:00 2014-11-21 18:00:00
0.01 0.02 0.03 0.04 0.06 0.07
两种方式:
动物园对象:
b<-aggregate(z, hour(index(z)), mean)
b
16 17 18
0.025 0.040 0.050
class (b)
[1] "zoo"
转换为数据帧:
a<-aggregate(z ~ hour(index(z)), FUN = 'mean')
a
hour(index(z)) z
1 16 0.025
2 17 0.040
3 18 0.050
class (a)
[1] "data.frame"
我有以下内容:
Lines <- "D1,Diff
1,20/11/2014 16:00,0.01
2,20/11/2014 17:00,0.02
3,20/11/2014 18:00,0.03
4,21/11/2014 16:00,0.04
5,21/11/2014 17:00,0.06
6,21/11/2014 18:00,0.07"
z <- read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")
ep <- endpoints(z,'hours')
a<-period.apply(z,ep,mean)
我想创建一个新的 Zoo 对象,每个小时都会有同一小时的 Diff 平均值。所需的结果将是:
16:00,0.025
17:00,0.03
18:00,0.05
内容一:
> a
2014-11-20 16:00:00 2014-11-20 17:00:00 2014-11-20 18:00:00 2014-11-21 16:00:00 2014-11-21 17:00:00 2014-11-21 18:00:00
0.01 0.02 0.03 0.04 0.06 0.07
两种方式:
动物园对象:
b<-aggregate(z, hour(index(z)), mean) b 16 17 18 0.025 0.040 0.050 class (b) [1] "zoo"
转换为数据帧:
a<-aggregate(z ~ hour(index(z)), FUN = 'mean') a hour(index(z)) z 1 16 0.025 2 17 0.040 3 18 0.050 class (a) [1] "data.frame"