簡體   English   中英

將R中的時間序列數據從秒轉換為小時均值

[英]Convert time-series data from seconds to hourly means in R

注意:我已經按照評論中的說明重新構造了上一個問題。

我正在使用三個不同的程序包,即dplyr,data.table和xts,將我的秒數據聚合為小時均值表示。 但是,令我驚訝的是,xts與其他兩個相比行為有所不同。 xts的問題是:

  • 與其他兩個相比,結果多了一個
  • 計算的每小時平均值與其他兩個小時完全不同

以下是用於測試目的的壓縮代碼:

library(xts)
library(data.table)
library(dplyr)
t2 <- as.POSIXct(seq(from = 1438367408, to = 1440959383, by = 30), origin = "1970-01-01")
dframe <- data.frame(timestamp=t2, power=rnorm(length(t2)))
#using xts
x <- xts(dframe$power,dframe$timestamp)
h1 <- period.apply(x, endpoints(x, "hours"), mean)
h1 <- data.frame(timestamp=trunc(index(h1),'hours'), power=coredata(h1))
#using data.table
h2 <- setDT(dframe)[, list(power= mean(power)) ,(timestamp= as.POSIXct(cut(timestamp, 'hours')))]
#using dpylr
h3 <- dframe %>% group_by(timestamp= as.POSIXct(cut(timestamp, 'hour'))) %>% summarise(power=mean(power))

關於規模的產出:

> dim(h1)
[1] 721   2
> dim(h2)
[1] 720   2
> dim(h3)
[1] 720   2

關於每小時的輸出表示:

> head(h1)
            timestamp       power
1 2015-08-01 00:00:00  0.04485894
2 2015-08-01 01:00:00 -0.02299071
> head(h2) # equals to head(h2)
             timestamp       power
1: 2015-08-01 00:00:00  0.10057538
2: 2015-08-01 01:00:00 -0.07456292

在h1情況下的額外觀察:

> tail(h1)
              timestamp        power
719 2015-08-30 22:00:00  0.069544538
720 2015-08-30 23:00:00  0.011673835
721 2015-08-30 23:00:00 -0.053858563

顯然,在一天的最后一小時有兩個觀察結果。 通常,應該只有一個。

我的系統信息:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.4.3      data.table_1.9.7 xts_0.9-7        zoo_1.7-12      

loaded via a namespace (and not attached):
 [1] lazyeval_0.1.10 magrittr_1.5    R6_2.1.1        assertthat_0.1  parallel_3.2.2  DBI_0.3.1       tools_3.2.2    
 [8] Rcpp_0.12.1     grid_3.2.2      chron_2.3-47    lattice_0.20-33

注意:

  • 原始數據集可以在鏈接中找到
  • 我想要一個解決此問題的方法,因為在我的實現方案中,xts比其余兩種方法快35倍

看來這可能是endpoints的錯誤,因為您的本地時區不是UTC的整整一個小時。 如果我將本地時區設置為您的時區,則可以復制該問題。

R> Sys.setenv(TZ="Asia/Kolkata")
R> x <- xts(dframe$power,dframe$timestamp)
R> h <- period.apply(x, endpoints(x, "hours"), mean)
R> head(h)
                        [,1]
2015-08-01 00:29:31 124.9055
2015-08-01 01:29:31 129.7197
2015-08-01 02:29:31 139.0899
2015-08-01 03:29:32 145.6592
2015-08-01 04:29:32 153.6840
2015-08-01 05:29:32 114.4809

請注意,端點以半小時為增量,而不是在小時結束時。 這是因為亞洲/加爾各答是UTC + 0530,並且endpoints按UTC表示的時間進行所有計算。

您可以通過將POSIXct對象的時區顯式設置為UTC來避免這種情況。

require(xts)
require(dplyr)
require(data.table)
Sys.setenv(TZ="Asia/Kolkata")

dframe <- read.csv("~/ap601.csv",head=TRUE,sep=",")
# set timezone on POSIXct object
dframe$timestamp <- as.POSIXct(dframe$timestamp, tz="UTC")

#using xts
x <- xts(dframe$power, dframe$timestamp)
h <- period.apply(x, endpoints(x, "hours"), mean)
h1 <- data.frame(timestamp=trunc(index(h),'hours'), power=coredata(h))
# using data.table
h2 <- setDT(dframe)[, list(power= mean(power)) ,(timestamp= cut(timestamp, 'hour'))]
# using dplyr
h3 <- dframe %>% group_by(timestamp= cut(timestamp, 'hour')) %>% summarise(power=mean(power))

all.equal(h1$power, h2$power)  # TRUE
all.equal(h1$power, h3$power)  # TRUE

這是一種在不將POSIXct列的時區設置為UTC的情況下獲得相同結果的解決方法。 請注意,這可能不適用於帶有夏令時的時區(亞洲/加爾各答未遵守任何夏令時)。

基本上,這個想法是在計算endpoints時從本地時間減去半小時,以便基礎UTC時間與小時對齊。

dframe <- read.csv("~/ap601.csv",head=TRUE,sep=",")
dframe$timestamp <- as.POSIXct(dframe$timestamp)

# subtract half an hour from the index when calculating endpoints
h <- period.apply(x, endpoints(index(x)-3600*0.5, 'hours'), mean)
h1 <- data.frame(timestamp=trunc(index(h),'hours'), power=coredata(h))
all.equal(h1$power, h2$power)  # TRUE
all.equal(h1$power, h3$power)  # TRUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM