在矩阵上进行最小/最大聚合后获取位置索引

时间:2019-06-27 07:16:59

标签: r max aggregate min

这是我在这里遇到的第一个问题(How to aggregate hourly values into 24h-average means without timestamp)。 现在,我想根据每个12小时间隔的时间序列来计算最大值(和最小值)。

我每小时都有一次数据测量(data_measure)。现在,我将其更改为半天的时间序列。

t_measure <- ts(data = data_measure, frequency = 12)

然后我使用了{stats}的聚合函数

data_measure_daily_max <- aggregate(t_measure, 1, max)

data_measure <- structure(c(8.29, 7.96, 8.14, 7.27, 7.37, 7.3, 7.23, 7.53, 
7.98, 10.2, 12.39, 14.34, 14.87, 14.39, 12.54, 11.84, 10.3, 10.62, 
10.65, 10.56, 10.43, 10.35, 9.85, 9.12, 8.95, 8.82, 8.92, 9.33, 
9.44, 9.3, 9.15, 9.37, 9.54, 10.24, 12.13, 12.43, 12.65, 13, 
13.18, 13.58, 13.64, 13.75, 13.85, 13.94, 13.79, 13.84, 13.94, 
14.26, 24.93, 24.64, 23.67, 21.46, 21.33, 20.83, 21.12, 21.1, 
23.75, 25.39, 30.72, 30.71, 30.81, 30.92, 32.61, 32.37, 32.49, 
30.68, 30.23, 30.45, 28.1, 26.9, 25.09, 25.07, 24.59, 24.22, 
23.05, 22.21, 22.07, 21.6, 21.24, 21.22, 21.85, 24.87, 28.85, 
29.42, 30.82, 30.97, 31.32, 30.81, 30.83, 29.9, 30.01, 30.31, 
30, 27.91, 25.78, 25.88, 8.78, 8.47, 8.49, 7.65, 8.63, 9.02, 
9.02, 8.11, 7.63, 9.19, 11.25, 12.24, 13.62, 12.09, 10.6, 11.1, 
10.16, 10.44, 9.58, 10.04, 10.01, 10.23, 9.51, 9.2, 9.34, 9.6, 
9.4, 9.45, 9.36, 9.26, 9.3, 9.46, 9.58, 9.89, 10.6, 11.04, 12.1, 
12.61, 13.12, 13.47, 13.55, 13.51, 13.63, 13.84, 13.93, 14.17, 
13.97, 13.86), .Dim = c(48L, 3L), .Dimnames = list(NULL, c("station1", 
"station2", "station3")))

所以实际上我需要一个索引/向量来告诉我这些时间间隔的最大值和最小值在哪里,因此稍后我可以准确地提取这些值以用于其他数据集进行比较。

我的第一次审判:

max_index <- which(aggregate(t_measure, 1, max))   # argument to 'which' is not logical

1 个答案:

答案 0 :(得分:3)

which.maxwhich.minaggregate一起使用

a1 <- aggregate(t_measure, 1, which.min)
a2 <- aggregate(t_measure, 1, which.max)

a1
#Time Series:
#Start = 1 
#End = 4 
#Frequency = 1 
#  station1 station2 station3
#1        7        6        9
#2       12       12       12
#3        2        8        6
#4        1       11        1

a2
#Time Series:
#Start = 1 
#End = 4 
#Frequency = 1 
#  station1 station2 station3
#1       12       11       12
#2        1        3        1
#3       12       12       12
#4       12        3       10

如果您想参考原始data_measure数据帧为分钟建立索引,我们可以

vals <- nrow(t_measure)/12 
index_min <- a1 + (12 * (seq_len(vals) - 1))
index_min

#Time Series:
#Start = 1 
#End = 4 
#Frequency = 1 
#  station1 station2 station3
#1        7        6        9
#2       24       24       24
#3       26       32       30
#4       37       47       37

station1的第1行的第7行中,可以看到data_measure的最大值,在接下来的12个小时的间隔中,在$(document).ready(function() { $('#example').DataTable(); } ); $( "#but" ).click(function() { var table = $('#example').DataTable(); var info = table.page(); console.log(info); table.page("first").draw("page"); var allData = table.rows().data(); var proof = allData.$('input').serialize(); table.page(info).draw("page"); console.log(proof); }); 的第7行中存在最大值,其他电台也是如此。