简体   繁体   English

将15分钟的时间序列数据汇总到每日

[英]aggregating 15minute time series data to daily

This is the data in my text file: (I have shown 10 rows out of 10,000) Index is the rownames, temp is time series and m are the values in mm. 这是我的文本文件中的数据:(我已经显示10,000中的10行)索引是行名,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"

Which I import in r using this: 我使用这个在r中导入:

    x2=read.table("data.txt", header=TRUE)

I tried using the following code for aggregating the time series to daily data : 我尝试使用以下代码将时间序列汇总到每日数据:

   c=aggregate(ts(x2[, 2], freq = 96), 1, mean)

I have set the frequency to 96 because for 15 min data 24 hrs will be covered in 96 values. 我将频率设置为96,因为对于15分钟的数据,24小时将覆盖96个值。

it returns me this: 它返回我这个:

    Time Series:
   Start = 1 
   End = 5 
   Frequency = 1 
   [1] 5366.698 5325.115 5311.969 5288.542 5331.115

But i want the same format in which I have my original data ie I also want the time series next to the values. 但是我想要原始数据的格式,即我也想要值旁边的时间序列。 I need help in achieving that. 我需要帮助来实现这一目标。

Use the apply.daily from the xts package after converting your data to an xts object: 将数据转换为xts对象后,请使用xts包中的apply.daily

Something like this should work: 这样的事情应该有效:

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

Update: Your problem in a reproducable format (with fake data) 更新:您的问题采用可复制的格式(带有伪造的数据)

We don't always need the actual dataset to be able to help troubleshoot.... 我们并不总是需要实际的数据集来帮助排除故障。

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

Update 2: A workaround alternative 更新2:替代方法

(Using the fake data I've provided in the above update.) (使用我在上述更新中提供的虚假数据。)

data.frame(time = x[seq(96, nrow(x), by=96), 1],
           mean = aggregate(ts(x[, 2], freq = 96), 1, mean))
#               time     mean
# 1 1970-01-01 23:45 3031.302
# 2 1970-01-02 23:45 3043.250
# 3 1970-01-03 23:45 2896.771
# 4 1970-01-04 23:45 2996.479

This would be a way to do it in base R: 这是在基数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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM