简体   繁体   English

在 r 中的时间序列中拆分时间间隔

[英]Split out time interval in time series in r

I have a dataset - time series Data below:我有一个数据集-时间序列数据如下:

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

I wish to select from the data the time of 11:00 every day I have tried doing a sequence but with daylight saving in GMT it changes to 12 in October fro 2019 and 2020 which is not correct我希望每天 11:00 的时间从数据中获取 select 我尝试过执行序列,但是在 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), ]

Using the xts object x shown reproducibly in the Note at the end:使用 xts object x在最后的注释中重复显示:

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

giving this xts object:给出这个 xts object:

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

Time zone problems are often specific to a particular installation but often the problem is between local time and GMT or due to the switch between standard and daylight savings time.时区问题通常是特定于特定安装的,但问题通常是在当地时间和 GMT 之间,或者是由于标准时间和夏令时之间的切换。 In these cases it often easiest to just set the entire session to GMT making the local time GMT.在这些情况下,通常最简单的方法是将整个 session 设置为GMT ,从而使本地时间为 GMT。 In that case there will be no confusion between local and GMT since they are both GMT and GMT does not have daylight savings time.在这种情况下,本地和 GMT 之间不会混淆,因为它们都是 GMT 并且 GMT 没有夏令时。

Sys.setenv(TZ = 'GMT')

Note笔记

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)

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

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