R - 从 csv 到 xts 的股票市场数据

发布于 2025-01-06 22:16:45 字数 537 浏览 1 评论 0原文

我在 CSV 中有这些数据:

Date  ALICORC1 ALT   ATACOBC1 AUSTRAC1 CONTINC1 BVN   DNT
40886 5.8      0.1   0.9      0.28     5.45     38.2  1.11
40889 5.8      0.1   0.88     0.28     5.37     37.7  1.04
40890 5.8      0.09  0.87     0.27     5.33     37.4  0.99
40891 5.7      0.1   0.85     0.27     5.3      37.5  0.91

这些是秘鲁股票市场的股票收盘价,我想将它们转换为 xts,以便我可以找到最佳投资组合和其他内容,但我找不到转换此 CSV 的方法到xts。我已经检查了这里许多问题的答案,但没有一个起作用。

我遇到的一些错误是:

  • 索引在数据行中有 XXXX 错误条目
  • 数据不明确。

有人可以帮助我吗?

I have this data in a CSV:

Date  ALICORC1 ALT   ATACOBC1 AUSTRAC1 CONTINC1 BVN   DNT
40886 5.8      0.1   0.9      0.28     5.45     38.2  1.11
40889 5.8      0.1   0.88     0.28     5.37     37.7  1.04
40890 5.8      0.09  0.87     0.27     5.33     37.4  0.99
40891 5.7      0.1   0.85     0.27     5.3      37.5  0.91

These are stock closing prices from the Peruvian Stock Market, and I want to convert them to xts so I can find the optimal portfolio and other stuff, but I can't find the way to convert this CSV to xts. I've checked out the answer to many of the questions here but none of them worked.

Some of the errors I've got are:

  • Index has XXXX bad entries at data rows
  • Ambiguous data.

Can anybody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2025-01-13 22:16:45

csv 代表逗号分隔值,因此问题中显示的布局不是 csv。我们假设数据确实是 csv 形式,而不是问题显示的形式。如果它确实是问题中显示的形式而不是 csv,则省略下面 read.zoo 中的 sep="," 参数。此外,如果存在其他偏差,您可能需要进一步修改参数。请参阅 ?read.zoo读取数据在 Zoo 包中的 Zoo 小插曲。

这里我们使用zoo包中的read.zoo将数据作为zoo对象z读入,然后将其转换为xts,x代码>.

请参阅 R News 4/1,其中专门处理了Excel 日期指出,如果使用 Mac 版本的 Excel,我们可能需要稍微修改下面的代码(如参考资料中所述)。

library(xts) # this also loads zoo which has read.zoo

toDate <- function(x) as.Date(x, origin = "1899-12-30")
z <- read.zoo("myfile.csv", header = TRUE, sep = ",", FUN = toDate)
x <- as.xts(z)

更新

动物园现在有 read.csv.zoo,因此可以编写 read.zoo 行:

z <- read.csv.zoo("myfile.csv", FUN = toDate)

csv stands for comma-separated-values so the layout shown in the question is not csv. We will assume that the data really is in csv form and not in the form shown the question. If it truly is in the form shown in the question rather than csv then omit the sep="," argument in read.zoo below. Also if there are other deviations you may need to modify the arguments further. See ?read.zoo and the Reading Data in Zoo vignette in the zoo package.

Here we use read.zoo in the zoo package to read in the data as a zoo object, z, and then we convert it to xts, x.

See R News 4/1 which specifically treats date handling of Excel dates noting that we may need to modify the code below slightly if the Mac version of Excel is being used (as described there in the reference).

library(xts) # this also loads zoo which has read.zoo

toDate <- function(x) as.Date(x, origin = "1899-12-30")
z <- read.zoo("myfile.csv", header = TRUE, sep = ",", FUN = toDate)
x <- as.xts(z)

Update

zoo now has read.csv.zoo so the read.zoo line could be written:

z <- read.csv.zoo("myfile.csv", FUN = toDate)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文