将1分钟的数据汇总成5分钟的平均数据

Vic*_*227 6 r date

我的问题是将每1分钟收集的数据汇总到5分钟的平均值.

DeviceTime         Concentration
6/20/2013 11:13       
6/20/2013 11:14
6/20/2013 11:15
6/20/2013 11:16
6/20/2013 11:17
6/20/2013 11:18
6/20/2013 11:19
6/20/2013 11:20
6/20/2013 11:21
6/20/2013 11:22
6/20/2013 11:23
6/20/2013 11:24
6/20/2013 11:25
6/20/2013 11:26
6/20/2013 11:27
6/20/2013 11:28
Run Code Online (Sandbox Code Playgroud)

...

我想要的结果是:

DeviceTime             Concentration
6/20/2013 11:15
6/20/2013 11:20
6/20/2013 11:25
6/20/2013 11:30
6/20/2013 11:35
...
Run Code Online (Sandbox Code Playgroud)

5分钟的平均值只是过去五分钟内浓度的简单平均值.

Mar*_*cok 19

如果您的数据不是在一个漂亮的5分钟挂钟边界上开始(如示例数据中所示 - 11:13),请注意cut()将根据它找到的第一个时间戳创建断点.这可能不是我们通常想要的.实际上,您的示例输出表明这不是您想要的.

这是做什么的cut():

df <- read.table(header=TRUE, sep=",", stringsAsFactors=FALSE, text="
DeviceTime,Concentration
6/20/2013 11:13,1
6/20/2013 11:14,1
6/20/2013 11:15,2
6/20/2013 11:16,2
6/20/2013 11:17,2
6/20/2013 11:18,2
6/20/2013 11:19,2
6/20/2013 11:20,3
6/20/2013 11:21,3
6/20/2013 11:22,3
6/20/2013 11:23,3
6/20/2013 11:24,3
6/20/2013 11:25,4")
df$DeviceTime <- as.POSIXct(df$DeviceTime, format="%m/%d/%Y %H:%M")

cut(df$DeviceTime, breaks="5 min")
 [1] 2013-06-20 11:13:00 2013-06-20 11:13:00 2013-06-20 11:13:00
 [4] 2013-06-20 11:13:00 2013-06-20 11:13:00 2013-06-20 11:18:00
 [7] 2013-06-20 11:18:00 2013-06-20 11:18:00 2013-06-20 11:18:00
[10] 2013-06-20 11:18:00 2013-06-20 11:23:00 2013-06-20 11:23:00
[13] 2013-06-20 11:23:00

means <- aggregate(df["Concentration"], 
                   list(fiveMin=cut(df$DeviceTime, "5 mins")),
                   mean)
means
              fiveMin Concentration
1 2013-06-20 11:13:00      1.600000
2 2013-06-20 11:18:00      2.600000
3 2013-06-20 11:23:00      3.333333
Run Code Online (Sandbox Code Playgroud)

请注意,第一行means(11:13:00条目)是前5行的平均值df,其时间为11:13到11:17 - 即直到下一个切割/断点之前11:18

如果您使用cut()以下内容,您将获得与dplyr相同的结果(即@ lukeA的答案):

df %>%
  group_by(DeviceTime = cut(DeviceTime, breaks="5 min")) %>%
  summarize(Concentration = mean(Concentration))
Source: local data frame [3 x 2]

           DeviceTime Concentration
1 2013-06-20 11:13:00      1.600000
2 2013-06-20 11:18:00      2.600000
3 2013-06-20 11:23:00      3.333333
Run Code Online (Sandbox Code Playgroud)

xts包似乎打破了挂钟时间:

require(xts)
df.xts <- xts(df$Concentration, df$DeviceTime)
means.xts <- period.apply(df.xts, endpoints(df.xts, "mins", k=5), mean)
means.xts
                    [,1]
2013-06-20 11:14:00    1
2013-06-20 11:19:00    2
2013-06-20 11:24:00    3
2013-06-20 11:25:00    4
Run Code Online (Sandbox Code Playgroud)

时间值始终是在5分钟窗口中找到的最后一次条目.align.time()如果要报告期末结束的时间,可以将时间索引列向上舍入下一个5分钟的边界:

means.rounded <- align.time(means.xts, 5*60)
means.rounded
                    [,1]
2013-06-20 11:15:00    1
2013-06-20 11:20:00    2
2013-06-20 11:25:00    3
2013-06-20 11:30:00    4
Run Code Online (Sandbox Code Playgroud)

如果要报告期间开始的时间,也可以向下舍入.但是你需要先定义自己的函数(我在Cross Validated中找到):

align.time.down = function(x,n) {
    index(x) = index(x) - n
    align.time(x,n)
}
means.rounded.down <- align.time.down(means.xts, 5*60)
means.rounded.down
                    [,1]
2013-06-20 11:10:00    1
2013-06-20 11:15:00    2
2013-06-20 11:20:00    3
2013-06-20 11:25:00    4
Run Code Online (Sandbox Code Playgroud)

另一个不使用xts包的解决方案,而是floor()如下:

df$DeviceTimeFloor <- as.POSIXct(floor(as.numeric(df$DeviceTime) / (5 * 60)) * (5 * 60), origin='1970-01-01')
meansFloor <- aggregate(Concentration ~ DeviceTimeFloor, df, mean)
meansFloor
      DeviceTimeFloor Concentration
1 2013-06-20 11:10:00             1
2 2013-06-20 11:15:00             2
3 2013-06-20 11:20:00             3
4 2013-06-20 11:25:00             4
Run Code Online (Sandbox Code Playgroud)

我更愿意报告5分钟间隔的开始时间 - floor()这对此有好处.因为,如果我按小时报告聚合,我希望2013-06-20 11:00:00的时间戳包含11:00:00 - 11:59:59而不是10:00:00的数据 - 10:59:59

如果您希望报告间隔的结束时间,ceiling()可以使用而不是floor().但请注意,时间戳11:01 - 11:05将转换为(并因此分组)在11:05 by ceiling().相比之下,floor()转换11:00 - 11:04到11:00.

所以他们每组都有不同的观察结果. xts包将对同一组观察进行分组floor(),但它将报告该时间段中最后一次观察的最后时间戳.


luk*_*keA 12

使用dplyr包并假设,您的数据存储在名为的数据框中df:

require(dplyr)
df %>%
  group_by(DeviceTime = cut(DeviceTime, breaks="5 min")) %>%
  summarize(Concentration = mean(Concentration))
Run Code Online (Sandbox Code Playgroud)