首页 > 解决方案 > 在r中的时间序列中拆分时间间隔

问题描述

我有一个数据集-时间序列数据如下:

Col 1(End): 
2018.01.01 01:00:00
2018.01.01 02:00:00
2018.01.01 03:00:00
2018.01.01 04:00:00
2018.01.01 05:00:00
2018.01.01 06:00:00
2018.01.01 07:00:00
2018.01.01 08:00:00
2018.01.01 09:00:00
2018.01.01 10:00:00
2018.01.01 11:00:00
2018.01.02 01:00:00
2018.01.02 02:00:00
2018.01.02 03:00:00
2018.01.02 04:00:00

Col 2(Price-indexed) 
55.09
44.02
44.0
33
43
43
33
33

我希望从数据中选择每天 11:00 的时间我已经尝试过执行序列,但是在 GMT 夏令时,它在 2019 年和 2020 年 10 月更改为 12,这是不正确的

datos_2019_2020<-read.csv("DayaheadPricesfull_2019_2020.csv")
#price variable changed to numeric

datos_2019_2020$Price_indexed=as.numeric(datos_2019_2020$Price)

time_index_2019_2020 <- seq(from = as.POSIXct("2019-01-01 00:00"), to = as.POSIXct("2020-12-31 23:00"), by = "hour",tz="GMT")

eventdata_2019_2020 <- as.xts(datos_2019_2020$Price_indexed, drop = FALSE,order.by = time_index_2019_2020)
df.new_2019_2020 = eventdata_2019_2020[seq(12, nrow(eventdata_2019_2020), 24), ]

标签: rtimesplit

解决方案


x使用末尾注释中可重复显示的 xts 对象:

x[format(time(x), format = "%H:%M:%S") == "11:00:00"]

给出这个 xts 对象:

                    [,1]
2018-01-01 11:00:00   NA

时区问题通常是特定于特定安装的,但问题通常是在当地时间和 GMT 之间,或者是由于标准时间和夏令时之间的切换。在这些情况下,通常最容易将整个会话设置为GMT使本地时间为 GMT。在这种情况下,本地和 GMT 之间不会混淆,因为它们都是 GMT 并且 GMT 没有夏令时。

Sys.setenv(TZ = 'GMT')

笔记

Lines1 <- "
2018.01.01 01:00:00
2018.01.01 02:00:00
2018.01.01 03:00:00
2018.01.01 04:00:00
2018.01.01 05:00:00
2018.01.01 06:00:00
2018.01.01 07:00:00
2018.01.01 08:00:00
2018.01.01 09:00:00
2018.01.01 10:00:00
2018.01.01 11:00:00
2018.01.02 01:00:00
2018.01.02 02:00:00
2018.01.02 03:00:00
2018.01.02 04:00:00"

Lines2 <- "
55.09
44.02
44.0
33
43
43
33
33"

library(xts)
col1 <- read.table(text = Lines1, sep = ",")
col2 <- read.table(text = Lines2)
# merge col1 and col2 using NA's to fill in
m <- merge(col1, col2, by = 0, all.x = TRUE)
z <- read.zoo(m[-1], tz = "", format = "%Y.%m.%d %H:%M:%S")
x <- as.xts(z)

推荐阅读