如何在 R 中的 quantmod 包中打印股权的日期?

发布于 2025-01-16 04:17:42 字数 300 浏览 4 评论 0原文

我想从 R 中的 quanatmod 包加载 Tesla 股票 5 年的历史记录。这样做我有:

tsla <- quantmod::getSymbols("TSLA", from = base::as.Date("2017-01-01"), to = base::as.Date("2022-01-31"), auto.assign = F)
tsla = as_tibble(tsla)
head(tsla)

但我还想要一个包含每个索引上的日期的列。如何在不添加日期的情况下完成此操作,因为这些价格对应于具体日期。

I want to load the 5 years history of Tesla equity from quanatmod package in R.Doing so I have :

tsla <- quantmod::getSymbols("TSLA", from = base::as.Date("2017-01-01"), to = base::as.Date("2022-01-31"), auto.assign = F)
tsla = as_tibble(tsla)
head(tsla)

But I also want a column with the dates on each index.How can this be done without just adding dates, because these prices correspond to specific dates .

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

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

发布评论

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

评论(2

梦里梦着梦中梦 2025-01-23 04:17:42

xts 输出转换为 data.frame 后,我们可以使用 rownames_to_column

library(dplyr)
library(tibble)
tsla %>% 
   as.data.frame %>% 
   rownames_to_column("Index")

-output

      Index TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
1 2017-01-03    42.972    44.066   42.192     43.398    29616500        43.398
2 2017-01-04    42.950    45.600   42.862     45.398    56067500        45.398
3 2017-01-05    45.284    45.496   44.390     45.350    29558500        45.350
4 2017-01-06    45.386    46.062   45.090     45.802    27639500        45.802
5 2017-01-09    45.794    46.384   45.600     46.256    19897500        46.256
6 2017-01-10    46.400    46.400   45.378     45.974    18300000        45.974
...

或者可以用 fortify.zoo 来完成

zoo::fortify.zoo(tsla)

We may use rownames_to_column after converting to data.frame from the xts output.

library(dplyr)
library(tibble)
tsla %>% 
   as.data.frame %>% 
   rownames_to_column("Index")

-output

      Index TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
1 2017-01-03    42.972    44.066   42.192     43.398    29616500        43.398
2 2017-01-04    42.950    45.600   42.862     45.398    56067500        45.398
3 2017-01-05    45.284    45.496   44.390     45.350    29558500        45.350
4 2017-01-06    45.386    46.062   45.090     45.802    27639500        45.802
5 2017-01-09    45.794    46.384   45.600     46.256    19897500        46.256
6 2017-01-10    46.400    46.400   45.378     45.974    18300000        45.974
...

Or it can be done with fortify.zoo

zoo::fortify.zoo(tsla)
二智少女猫性小仙女 2025-01-23 04:17:42

只需打印 TSLA xts 对象即可。 ( fortify.zoo(TSLA) 将其转换为第一列中带有索引的数据框,但只是打印它并不真正需要。)

library(quantmod)
getSymbols("TSLA", from = "2017-01-01", to = "2022-01-31")
TSLA

给出:

           TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
2017-01-03    42.972    44.066   42.192     43.398    29616500        43.398
2017-01-04    42.950    45.600   42.862     45.398    56067500        45.398
2017-01-05    45.284    45.496   44.390     45.350    29558500        45.350
2017-01-06    45.386    46.062   45.090     45.802    27639500        45.802
2017-01-09    45.794    46.384   45.600     46.256    19897500        46.256
2017-01-10    46.400    46.400   45.378     45.974    18300000        45.974
...etc...

Just print the TSLA xts object. ( fortify.zoo(TSLA) converts it to a data frame with the index in the first column but isn't really needed for simply printing it.)

library(quantmod)
getSymbols("TSLA", from = "2017-01-01", to = "2022-01-31")
TSLA

giving:

           TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
2017-01-03    42.972    44.066   42.192     43.398    29616500        43.398
2017-01-04    42.950    45.600   42.862     45.398    56067500        45.398
2017-01-05    45.284    45.496   44.390     45.350    29558500        45.350
2017-01-06    45.386    46.062   45.090     45.802    27639500        45.802
2017-01-09    45.794    46.384   45.600     46.256    19897500        46.256
2017-01-10    46.400    46.400   45.378     45.974    18300000        45.974
...etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文