## load library
library(cryptoQuotes)
If you want to use the simplicity of {tidyverse} or the power of {data.table}, the {xts}-object can be easily converted.
However, its important to maintain data integrity, especially, if the date and time zone is important for you.
Converting xts and zoo to tibble
Converting to tibble
requires a few steps to achieve the
same data structure as the xts
,
# 1) load pipe
library(magrittr)
# 2) convert to tibble
# using as_tibble
tbl <- tibble::as_tibble(
x = cbind(
index = zoo::index(ATOM),
zoo::coredata(ATOM)
)
) %>% dplyr::mutate(
index = lubridate::as_datetime(
x = index,
tz = "Europe/Copenhagen"
)
)
# 3) tail data
head(tbl, 3)
#> # A tibble: 3 × 6
#> index open high low close volume
#> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 2023-12-30 00:00:00 10.9 10.9 10.9 10.9 3260.
#> 2 2023-12-30 00:15:00 10.9 11.0 10.9 11.0 1863.
#> 3 2023-12-30 00:30:00 11.0 11.0 10.9 11.0 1861.
Converting xts and zoo to data.table
Converting to a data.table
is straightforward as
as.data.table()
handles everything under the hood,
# 1) convert to data.table
# using as.data.table
DT <- data.table::as.data.table(
ATOM
)
# 2) head data
head(DT, 3)
#> index open high low close volume
#> <POSc> <num> <num> <num> <num> <num>
#> 1: 2023-12-30 00:00:00 10.8778 10.9133 10.8775 10.9089 3259.760
#> 2: 2023-12-30 00:15:00 10.9089 10.9530 10.9008 10.9512 1862.697
#> 3: 2023-12-30 00:30:00 10.9512 10.9884 10.9373 10.9832 1861.493
Checking data integrity
Checking date integrity
It is important that the date.time has not been converted to a different timezone in the process, without explicitly coding it as such,