如何使用 xts 对象往返(往返于 .csv 文件)?

问题描述 投票:0回答:2

尝试使用 xts 对象进行往返(写入/读取)。 read.zoo() 返回一个数据框!将其转换为 xts 失败。

如何使其成为 xts 对象?

library(quantmod)
from <- "2016-01-01" ## leap year
to <- "2017-01-01"
symbol <- "AAPL"
getSymbols(symbol, from=from, to=to)
class(AAPL)
write.zoo(AAPL, file="newapple.txt", sep=",")
x <- read.zoo("newapple.txt")
class(x)
x <- as.xts(x, RECLASS=TRUE) ## has errors
# write.zoo(x, file="newerapple.txt", sep=",") ## crashes
r time-series xts zoo quantmod
2个回答
0
投票

运行

read.zoo()
行时出现错误。

x <- read.zoo("newapple.txt")
> Error in read.zoo("newapple2.txt") : 
  index has 253 bad entries at data rows: 1 2 3 ...

使用

readr::write_csv()
readr::read_csv()
,我完成了往返。

library(quantmod)
library(tidyverse)
library(zoo)

from <- "2016-01-01" # leap year
to <- "2017-01-01"
symbol <- "AAPL"
getSymbols(symbol, from = from, to = to)
#> [1] "AAPL"
class(AAPL)
#> [1] "xts" "zoo"

AAPL |>
  as_tibble(rownames = "index") |>
  write_csv("newapple2.txt")

x <- read_csv("newapple2.txt")

class(x)
#> [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"

as.xts(x, RECLASS = TRUE)
#>            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#> 2016-01-04   25.6525   26.3425  25.5000    26.3375   270597600      24.00906
#> 2016-01-05   26.4375   26.4625  25.6025    25.6775   223164000      23.40741
#> 2016-01-06   25.1400   25.5925  24.9675    25.1750   273829600      22.94933
#> 2016-01-07   24.6700   25.0325  24.1075    24.1125   324377600      21.98078
#> 2016-01-08   24.6375   24.7775  24.1900    24.2400   283192000      22.09699
#> 2016-01-11   24.7425   24.7650  24.3350    24.6325   198957600      22.45480
#> 2016-01-12   25.1375   25.1725  24.7100    24.9900   196616800      22.78069
#> 2016-01-13   25.0800   25.2975  24.3250    24.3475   249758400      22.19499
#> 2016-01-14   24.4900   25.1200  23.9350    24.8800   252680400      22.68042
#> 2016-01-15   24.0500   24.4275  23.8400    24.2825   319335600      22.13574
#>        ...                                                                  
#> 2016-12-16   29.1175   29.1250  28.9125    28.9925   177404400      27.01738
#> 2016-12-19   28.9500   29.3450  28.9375    29.1600   111117600      27.17346
#> 2016-12-20   29.1850   29.3750  29.1700    29.2375    85700000      27.24568
#> 2016-12-21   29.2000   29.3500  29.1950    29.2650    95132800      27.27131
#> 2016-12-22   29.0875   29.1275  28.9100    29.0725   104343600      27.09193
#> 2016-12-23   28.8975   29.1300  28.8975    29.1300    56998000      27.14550
#> 2016-12-27   29.1300   29.4500  29.1225    29.3150    73187600      27.31790
#> 2016-12-28   29.3800   29.5050  29.0500    29.1900    83623600      27.20142
#> 2016-12-29   29.1125   29.2775  29.1000    29.1825    60158000      27.19443
#> 2016-12-30   29.1625   29.3000  28.8575    28.9550   122345200      26.98243

0
投票

需要

row.names=TRUE)

> library(quantmod)
> from <- "2016-01-01" # leap year
> to <- "2017-01-01"
> symbol <- "AAPL"
> getSymbols(symbol, from=from, to=to)
[1] "AAPL"
> class(AAPL)
[1] "xts" "zoo"
> write.zoo(AAPL, file="newapple.txt", row.names=TRUE)
> x <- read.zoo("newapple.txt")
> class(x)
[1] "zoo"
> as.xts(x, RECLASS=TRUE)
           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2016-01-04   25.6525   26.3425  25.5000    26.3375   270597600      24.00906
2016-01-05   26.4375   26.4625  25.6025    25.6775   223164000      23.40741
2016-01-06   25.1400   25.5925  24.9675    25.1750   273829600      22.94934
2016-01-07   24.6700   25.0325  24.1075    24.1125   324377600      21.98077
2016-01-08   24.6375   24.7775  24.1900    24.2400   283192000      22.09700
2016-01-11   24.7425   24.7650  24.3350    24.6325   198957600      22.45481
2016-01-12   25.1375   25.1725  24.7100    24.9900   196616800      22.78069
2016-01-13   25.0800   25.2975  24.3250    24.3475   249758400      22.19500
2016-01-14   24.4900   25.1200  23.9350    24.8800   252680400      22.68042
2016-01-15   24.0500   24.4275  23.8400    24.2825   319335600      22.13574
       ...                                                                  
2016-12-16   29.1175   29.1250  28.9125    28.9925   177404400      27.01737
2016-12-19   28.9500   29.3450  28.9375    29.1600   111117600      27.17347
2016-12-20   29.1850   29.3750  29.1700    29.2375    85700000      27.24568
2016-12-21   29.2000   29.3500  29.1950    29.2650    95132800      27.27131
2016-12-22   29.0875   29.1275  28.9100    29.0725   104343600      27.09192
2016-12-23   28.8975   29.1300  28.8975    29.1300    56998000      27.14550
2016-12-27   29.1300   29.4500  29.1225    29.3150    73187600      27.31790
2016-12-28   29.3800   29.5050  29.0500    29.1900    83623600      27.20142
2016-12-29   29.1125   29.2775  29.1000    29.1825    60158000      27.19443
2016-12-30   29.1625   29.3000  28.8575    28.9550   122345200      26.98243
© www.soinside.com 2019 - 2024. All rights reserved.