R 将15分钟的时间序列数据聚合到每日

R 将15分钟的时间序列数据聚合到每日,r,time-series,zoo,R,Time Series,Zoo,这是我文本文件中的数据:(我显示了10000行中的10行) Index是行名,temp是时间序列,m是以mm为单位的值 "Index" "temp" "m" 1 "2012-02-07 18:15:13" "4297" 2 "2012-02-07 18:30:04" "4296" 3 "2012-02-07 18:45:10" "4297" 4 "2012-02-07 19:00:01" "4297" 5 "2012-02-07 19:15:07" "42

这是我文本文件中的数据:(我显示了10000行中的10行) Index是行名,temp是时间序列,m是以mm为单位的值

     "Index" "temp" "m"
   1 "2012-02-07 18:15:13" "4297"
   2 "2012-02-07 18:30:04" "4296"
   3 "2012-02-07 18:45:10" "4297"
   4 "2012-02-07 19:00:01" "4297"
   5 "2012-02-07 19:15:07" "4298"
   6 "2012-02-07 19:30:13" "4299"
   7 "2012-02-07 19:45:04" "4299"
   8 "2012-02-07 20:00:10" "4299"
   9 "2012-02-07 20:15:01" "4300"
   10 "2012-02-07 20:30:07" "4301"
我使用以下方法在r中导入:

    x2=read.table("data.txt", header=TRUE)
我尝试使用以下代码将时间序列聚合到每日数据:

   c=aggregate(ts(x2[, 2], freq = 96), 1, mean)
我将频率设置为96,因为对于15分钟的数据,24小时将包含在96个值中

它返回给我这个:

    Time Series:
   Start = 1 
   End = 5 
   Frequency = 1 
   [1] 5366.698 5325.115 5311.969 5288.542 5331.115
但是我想要和原始数据相同的格式,也就是说,我还想要值旁边的时间序列。
我需要帮助来实现这一点。

将数据转换为
xts
对象后,使用
xts
包中的
apply.daily

像这样的方法应该会奏效:

x2 = read.table(header=TRUE, text='     "Index" "temp" "m"
1 "2012-02-07 18:15:13" "4297"
2 "2012-02-07 18:30:04" "4296"
3 "2012-02-07 18:45:10" "4297"
4 "2012-02-07 19:00:01" "4297"
5 "2012-02-07 19:15:07" "4298"
6 "2012-02-07 19:30:13" "4299"
7 "2012-02-07 19:45:04" "4299"
8 "2012-02-07 20:00:10" "4299"
9 "2012-02-07 20:15:01" "4300"
10 "2012-02-07 20:30:07" "4301"')

x2$temp = as.POSIXct(strptime(x2$temp, "%Y-%m-%d %H:%M:%S"))
require(xts)
x2 = xts(x = x2$m, order.by = x2$temp)
apply.daily(x2, mean)
##                       [,1]
## 2012-02-07 20:30:07 4298.3
更新:您的问题以可复制的格式出现(使用假数据) 我们并不总是需要实际的数据集来帮助解决问题

set.seed(1) # So you can get the same numbers as I do
x = data.frame(datetime = seq(ISOdatetime(1970, 1, 1, 0, 0, 0), 
                              length = 384, by = 900), 
               m = sample(2000:4000, 384, replace = TRUE))
head(x)
#              datetime    m
# 1 1970-01-01 00:00:00 2531
# 2 1970-01-01 00:15:00 2744
# 3 1970-01-01 00:30:00 3146
# 4 1970-01-01 00:45:00 3817
# 5 1970-01-01 01:00:00 2403
# 6 1970-01-01 01:15:00 3797
require(xts)
x2 = xts(x$m, x$datetime)
head(x2)
#                     [,1]
# 1970-01-01 00:00:00 2531
# 1970-01-01 00:15:00 2744
# 1970-01-01 00:30:00 3146
# 1970-01-01 00:45:00 3817
# 1970-01-01 01:00:00 2403
# 1970-01-01 01:15:00 3797
apply.daily(x2, mean)
#                         [,1]
# 1970-01-01 23:45:00 3031.302
# 1970-01-02 23:45:00 3043.250
# 1970-01-03 23:45:00 2896.771
# 1970-01-04 23:45:00 2996.479
更新2:解决方案替代方案 (使用我在上述更新中提供的虚假数据。)


这将是在base R中执行此操作的一种方法:

x2 <- within(x2, {
   temp <- as.POSIXct(temp, format='%Y-%m-%d %H:%M:%S')
   days <- as.POSIXct(cut(temp, breaks='days'))
   m <- as.numeric(m)
})

with(x2, aggregate(m, by=list(days=days), mean))

x2请举一个可重复的例子:我已经编辑了我的问题。如果您需要更多的信息,请告诉我。也许您应该先将数据读入数据框,然后再输出内容。@John-对不起,输出的数据很长,我认为它没有用。您只需复制我的文本文件的内容并使用它即可。@andrie-您只需将我提供的数据复制粘贴到文本文件并使用该文本文件即可。我尝试使用dput(),但它产生了很长的输出。请帮帮我
x2 <- within(x2, {
   temp <- as.POSIXct(temp, format='%Y-%m-%d %H:%M:%S')
   days <- as.POSIXct(cut(temp, breaks='days'))
   m <- as.numeric(m)
})

with(x2, aggregate(m, by=list(days=days), mean))