半小时时间戳窗口中的平均字段 [英] Average of field in half-an-hour window of timestamps
问题描述
我的数据框具有列名Timestamp, es
,看起来像:
My dataframe has column-names Timestamp, es
and looks like:
Timestamp es
2015-04-01 09:07:42 31
2015-04-01 09:08:01 29.5
2015-04-01 09:15:03 18.5
2015-04-01 09:15:05 8.8
2015-04-01 09:15:09 9.6
时间持续到15:30:30(每天相对于每个时间戳大约12000 es
个数据点)和相应的es.
The time runs till 15:30:30 (around 12000 es
data points against each timestamp a day) and the corresponding es.
R在某些软件包或代码中是否具有某些功能,可以平均半小时内所有时间戳的es
.示例输出应如下所示:
Does R have some function in some package or code to average the es
of all the timestamps within half hour. Sample output should look like:
2015-04-01 09:30:00 Value(Average of all es from 9:00 to 9:30)
2015-04-01 10:00:00 Value(Average of all es from 9:30 to 10:00)
2015-04-01 10:30:00 Value(Average of all es from 10:00 to 10:30)
... (the list goes on till 15:30:30)
推荐答案
如smci警告,您添加的有关数据的信息越少,帮助就越有限.这是一种base R
方法,从提供的日期开始间隔30分钟.因此,空间隔将不会出现(取决于您希望的输出是否有帮助).聚合函数将平均值应用所需的组间隔.我将您的示例扩展为包含更多测试间隔:
As smci warned, the less information you add about your data, the more limited the help can be. This is a base R
approach that creates 30 minute intervals from the dates provided. Therefore, empty intervals will not appear (depending on your desired output this can help or not). The aggregate function applies the mean by the desired group intervals. I expanded your example to incorporate more test intervals:
cuts <- seq(round(min(df$Timestamp), "hours"), max(df$Timestamp)+30*60, "30 min")
aggregate(df$es, list(cut(df$Timestamp, cuts)), mean)
# Group.1 x
#1 2015-04-01 09:00:00 31.00
#2 2015-04-01 10:00:00 29.50
#3 2015-04-01 11:00:00 13.65
#4 2015-04-01 13:00:00 9.60
数据
df <- structure(list(Timestamp = structure(c(1427893662, 1427897281,
1427901303, 1427901605, 1427908509), class = c("POSIXct", "POSIXt"
), tzone = ""), es = c(31, 29.5, 18.5, 8.8, 9.6)), .Names = c("Timestamp",
"es"), row.names = c(NA, -5L), class = "data.frame")
这篇关于半小时时间戳窗口中的平均字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!