r - 选择要在 R 中操作的 xts 列
问题描述
我有以下标题:
"Index","Open","High","Low","Close","Volume"
我只想在名为价格的表中使用收盘量,我如何只选择收盘列所以我只能使用该数据?
我尝试了一些类似的东西
closePrices <- prices("Close")
和
closePrices <- prices[Close]
两者都抛出错误
解决方案
有几个选项可以做到这一点。请参阅下面的示例。
library(xts)
library(quantmod)
data(sample_matrix)
sample_xts <- as.xts(sample_matrix)
基于列名
xts_close <- sample_xts[, "Close"]
head(xts_close)
Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185
或者
xts_close <- sample_xts$Close
head(xts_close)
Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185
使用 quantmod
# using quantmod::Cl
quant_close <- Cl(sample_xts)
head(quant_close)
Close
2007-01-02 50.11778
2007-01-03 50.39767
2007-01-04 50.33236
2007-01-05 50.33459
2007-01-06 50.18112
2007-01-07 49.99185
all.equal(xts_close, quant_close)
[1] TRUE
推荐阅读
- java - 将嵌套 XML 转换为 Java 对象的正确方法
- oracle - 如何防止sql注入oracle apex
- python - 为什么当满足 if 语句条件时我的代码返回 False?
- vuetify.js - 包括 Vuetify
代替 环形? - amazon-cloudformation - 我需要帮助了解 cdk 中的 AwsCustomResource.policy 是什么?
- web-scraping - 如何使用 Xidel 从文件中选择要抓取的行?
- json - 为 json 类型“double”创建模型时显示此错误不是颤振中“int”类型的子类型?
- php - 自重定向脚本而不是 cron 作业
- c - 从数组和列表中删除项目的时间复杂度
- python - 在python循环中导入文件以进行数据处理