Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将xts timeseries转换为00:00:00的小时差_R_Timestamp_Sequence_Xts - Fatal编程技术网

如何将xts timeseries转换为00:00:00的小时差

如何将xts timeseries转换为00:00:00的小时差,r,timestamp,sequence,xts,R,Timestamp,Sequence,Xts,我有一个xts timeseries作为 Timestamp - X 30-07-2019 23:00:00 - 110 31-07-2019 00:00:00 - 120 31-07-2019 01:00:00 - 105 31-07-2019 02:00:00 - 110 31-07-2019 03:00:00 - 100 31-07-2019 04:00:00 - 105 31-07-2019 05:00:00 - 115 31-07-2019 06:00:0

我有一个xts timeseries作为

Timestamp           - X
30-07-2019 23:00:00 - 110
31-07-2019 00:00:00 - 120 
31-07-2019 01:00:00 - 105
31-07-2019 02:00:00 - 110 
31-07-2019 03:00:00 - 100 
31-07-2019 04:00:00 - 105 
31-07-2019 05:00:00 - 115 
31-07-2019 06:00:00 - 125
现在我希望时间戳是小时,例如,是一天中的小时

Timestamp - X
-1        - 110
0         - 120 
1         - 105
2         - 110 
3         - 100 
4         - 105 
5         - 115 
6         - 125

并且仍然将其作为xts系列保存,以便在闪亮的应用程序中与dygraph一起使用

始终尝试以
dput
或我制作数据的方式提供数据

这是一步一步的方法

Timestamp <- c("30-07-2019 23:00:00", "31-07-2019 00:00:00", "31-07-2019 01:00:00", "31-07-2019 02:00:00", "31-07-2019 03:00:00", "31-07-2019 04:00:00", "31-07-2019 05:00:00", "31-07-2019 06:00:00")
X <- c("-110","- 120","- 105","- 110","- 100","- 105","- 115","- 125")

#DF creation
df <- data.frame(Timestamp = Timestamp,X = X)

#Extracting `time` like `23:00:00`      
df$Timestamp <- sub(".* ", "", df$Timestamp)

#extracting only hours  
df$Timestamp <- format(strptime(df$Timestamp, format='%H:%M:%S'), '%H')

#converting into `Numeric` or `integer`
df$Timestamp <- as.integer(df$Timestamp)

#finally loop to get the desired result
for(i in df$Timestamp){
    if(i <= 12){
        df[,1][which(df$Timestamp == i)] <- i
    } else if(i > 12) {
        df[,1][which(df$Timestamp == i)] <- i-24
        } 
}
编辑:简洁的方式,这也应该有效

 df <- data.frame(Timestamp = Timestamp,X = X)

 #applying `POSIXct`
 df$Timestamp <- as.integer(format(as.POSIXct(df$Timestamp, format="%d-%m-%Y %H:%M:%S"), "%H"))

 #loop
 df$Timestamp <- ifelse(df$Timestamp<=12,df$Timestamp,df$Timestamp-24)

df另一种选择是使用
lubridate
方法:

library(lubridate)
hours_from_midnight <- hour(dmy_hms(df$Timestamp))
# boolean vector indicating if the time mome
after_midday <- pm(dmy_hms(df$Timestamp))
# coercion boolean to numerical is used
df$hour <- hours_from_midnight - 24 * after_midday
库(lubridate)

但是请注意,这可能不适用于动态图,因为df$hour不是DFT的索引xts“index”(“timestamp”)不能简单地表示一天中的小时。它需要是日期时间格式。您可以在xts对象中创建一个数字列来存储时间戳的小时数。另外,制作一个data.frame/data.table用于输入动态图
library(lubridate)
hours_from_midnight <- hour(dmy_hms(df$Timestamp))
# boolean vector indicating if the time mome
after_midday <- pm(dmy_hms(df$Timestamp))
# coercion boolean to numerical is used
df$hour <- hours_from_midnight - 24 * after_midday