xts

Published by onesixx on

xts Converting

https://statkclee.github.io/finance/finance-ts-xts.html

data.table –> xts

as.xts.data.table() 첫번째 컬럼이 Date나 POSIXct 여야한다.

dt0 <- data.table(day = ymd(c("2001-01-01","2001-01-02","2001-01-03")),
                  val = rnorm(3) %>% round(1))
dtxts <- dt0 %>% as.xts.data.table()
#             val
# 2001-01-01 -0.1
# 2001-01-02 -0.3
# 2001-01-03 -0.1
ts_data.table(dtxts) 
dfxts %>% coredata()
#      [,1]
# [1,] -0.8
# [2,]  0.7
# [3,]  1.3
dfxts %>% index()
# [1] "2001-01-01" "2001-01-02" "2001-01-03"

반대로 as.data.table.xts()로 as.data.table(x, keep.rownames = TRUE, key=NULL, …) 을 사용

data.frame –> xts

df0 <- data.frame(day = ymd(c("2001-01-01","2001-01-02","2001-01-03")),
                  val = rnorm(3) %>% round(1))
dfxts <- df0[,-1] %>% as.xts(order.by=df0[,1])

matrix –> xts

mx <- matrix(1:6, ncol=2, nrow=3)
mxxts <- mx %>% as.xts(order.by=Sys.Date()+1:3)
#            [,1] [,2]
# 2021-01-11    1    4
# 2021-01-12    2    5
# 2021-01-13    3    6

ts –> xts

ts <- AirPassengers
tsxts <-  ts%>% as.xts()  #        from xts
tsxts <-  ts%>% ts_xts()  # same , from tsbox  ts==>something.
#          [,1]
# Jan 1949  112
# Feb 1949  118
# Mar 1949  132
# ...
# Nov 1960  390
# Dec 1960  432

csv –> data.tble –> xts

URL <- "https://raw.githubusercontent.com/statsmodels/statsmodels/master/statsmodels/datasets/sunspots/sunspots.csv"
sunspot <- fread(URL)
sunspot[ , YEAR:=ymd(YEAR, truncated=2L)]

sunspot_xts0 <- as.xts.data.table(sunspot)
sunspot_xts1 <- as.xts( sunspot$SUNACTIVITY, order.by=sunspot$YEAR )
#             [,1]
# 1700-01-01   5.0
# 1701-01-01  11.0
# ...
# 2007-01-01   7.5
# 2008-01-01   2.9

ex> data.table

library(timeDate)

DT0 = structure(
\tlist(
\t\tX=1:6, 
\t\tDate=structure(c(1L,1L,1L,1L,1L,1L), .Label="07/01/1998", class="factor"),
\t\tTime=structure(1:6, .Label=c("06:31","06:34","06:35","06:36","06:38","06:39"), class="factor"), 
        Open= c(14.06, 14.11, 14.06, 14.09, 14.09, 14.06), 
        High= c(14.06, 14.13, 14.13, 14.09, 14.09, 14.13), 
        Low = c(14,    14.06, 14.06, 14.03, 14.06, 14.06), 
\t\tClose=c(14,    14.06, 14.13, 14.03, 14.06, 14.13),
\t\tVolume=c(257600L, 24400L, 2500L, 900L, 3000L, 16700L)
\t), 
\t.Names = c("X","Date","Time","Open","High","Low","Close","Volume"), 
\tclass="data.frame",
\trow.names=c(NA, -6L)
) %>% as.data.table()

DT0_xts <- xts(DT0[,4:8],
               order.by=as.POSIXct( strptime(str_c(DT0$Date," ", DT0$Time), format="%m/%d/%Y %H:%M") )
           )

#                      Open  High   Low Close Volume
# 1998-07-01 06:31:00 14.06 14.06 14.00 14.00 257600
# 1998-07-01 06:34:00 14.11 14.13 14.06 14.06  24400
# 1998-07-01 06:35:00 14.06 14.13 14.06 14.13   2500
# 1998-07-01 06:36:00 14.09 14.09 14.03 14.03    900
# 1998-07-01 06:38:00 14.09 14.09 14.06 14.06   3000
# 1998-07-01 06:39:00 14.06 14.13 14.06 14.13  16700
Categories: R ts

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x