使用午夜列中的日期和秒数将 data.frame 转换为 xts 对象 (R)
Using date and seconds from midnight columns to convert a data.frame to an xts object (R)
假设我有这样的数据:
DATE TIME Col1 Col2
1 1993-01-04 34538 10.250 10.000
2 1994-01-05 34541 10.250 10.111
3 1997-03-16 34546 10.250 10.222
4 2017-11-10 34561 10.251 10.333
5 2001-08-28 34565 10.251 10.444
6 2006-04-20 34807 10.251 10.555
'TIME' 列的格式为从午夜算起的秒数。我将如何组合 'DATE' 和 'TIME' 列以获得看起来像这样的 xts
对象:
Col1 Col2
X1993.01.04.09.35.38 10.250 10.000
X1994.01.05.09.35.41 10.250 10.111
X1997.03.16.09.35.46 10.250 10.222
X2017.11.10.09.36.01 10.251 10.333
X2001.08.28.09.36.05 10.251 10.444
X2006.04.20.09.40.07 10.251 10.555
我们可以通过指定 order.by
[=15= 将 'DATE'、'TIME' 列转换为日期时间 class 并将数据集转换为 xts
]
library(xts)
library(lubridate)
xts(df1[-(1:2)], order.by = as.POSIXct(paste(df1$DATE,
hms::hms(seconds_to_period(df1$TIME)))))
# Col1 Col2
#1993-01-04 09:35:38 10.250 10.000
#1994-01-05 09:35:41 10.250 10.111
#1997-03-16 09:35:46 10.250 10.222
#2001-08-28 09:36:05 10.251 10.444
#2006-04-20 09:40:07 10.251 10.555
#2017-11-10 09:36:01 10.251 10.333
注意:xts
的索引需要一个 Datetime class 对象而不是格式化字符 class vector
数据
df1 <- structure(list(DATE = c("1993-01-04", "1994-01-05", "1997-03-16",
"2017-11-10", "2001-08-28", "2006-04-20"), TIME = c(34538L, 34541L,
34546L, 34561L, 34565L, 34807L), Col1 = c(10.25, 10.25, 10.25,
10.251, 10.251, 10.251), Col2 = c(10, 10.111, 10.222, 10.333,
10.444, 10.555)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6"))
我不是 xts
专业人士,但我认为第一步是将这两列转换为 POSIXt
对象。
as.POSIXct(dat$DATE, tz = "UTC", format = "%Y-%m-%d") + dat$TIME
# [1] "1993-01-04 09:35:38 UTC" "1994-01-05 09:35:41 UTC"
# [3] "1997-03-16 09:35:46 UTC" "2017-11-10 09:36:01 UTC"
# [5] "2001-08-28 09:36:05 UTC" "2006-04-20 09:40:07 UTC"
(顺便说一句:假设采用 "%H.%M.%S"
格式,我认为您没有 36 分 61 秒...)
假设我有这样的数据:
DATE TIME Col1 Col2
1 1993-01-04 34538 10.250 10.000
2 1994-01-05 34541 10.250 10.111
3 1997-03-16 34546 10.250 10.222
4 2017-11-10 34561 10.251 10.333
5 2001-08-28 34565 10.251 10.444
6 2006-04-20 34807 10.251 10.555
'TIME' 列的格式为从午夜算起的秒数。我将如何组合 'DATE' 和 'TIME' 列以获得看起来像这样的 xts
对象:
Col1 Col2
X1993.01.04.09.35.38 10.250 10.000
X1994.01.05.09.35.41 10.250 10.111
X1997.03.16.09.35.46 10.250 10.222
X2017.11.10.09.36.01 10.251 10.333
X2001.08.28.09.36.05 10.251 10.444
X2006.04.20.09.40.07 10.251 10.555
我们可以通过指定 order.by
[=15= 将 'DATE'、'TIME' 列转换为日期时间 class 并将数据集转换为 xts
]
library(xts)
library(lubridate)
xts(df1[-(1:2)], order.by = as.POSIXct(paste(df1$DATE,
hms::hms(seconds_to_period(df1$TIME)))))
# Col1 Col2
#1993-01-04 09:35:38 10.250 10.000
#1994-01-05 09:35:41 10.250 10.111
#1997-03-16 09:35:46 10.250 10.222
#2001-08-28 09:36:05 10.251 10.444
#2006-04-20 09:40:07 10.251 10.555
#2017-11-10 09:36:01 10.251 10.333
注意:xts
的索引需要一个 Datetime class 对象而不是格式化字符 class vector
数据
df1 <- structure(list(DATE = c("1993-01-04", "1994-01-05", "1997-03-16",
"2017-11-10", "2001-08-28", "2006-04-20"), TIME = c(34538L, 34541L,
34546L, 34561L, 34565L, 34807L), Col1 = c(10.25, 10.25, 10.25,
10.251, 10.251, 10.251), Col2 = c(10, 10.111, 10.222, 10.333,
10.444, 10.555)), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6"))
我不是 xts
专业人士,但我认为第一步是将这两列转换为 POSIXt
对象。
as.POSIXct(dat$DATE, tz = "UTC", format = "%Y-%m-%d") + dat$TIME
# [1] "1993-01-04 09:35:38 UTC" "1994-01-05 09:35:41 UTC"
# [3] "1997-03-16 09:35:46 UTC" "2017-11-10 09:36:01 UTC"
# [5] "2001-08-28 09:36:05 UTC" "2006-04-20 09:40:07 UTC"
(顺便说一句:假设采用 "%H.%M.%S"
格式,我认为您没有 36 分 61 秒...)