如何在 Quantmod 中处理雅虎财经股票代码中的连字符 [英] How to handle hyphens in yahoo finance tickers in Quantmod

查看:41
本文介绍了如何在 Quantmod 中处理雅虎财经股票代码中的连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行以下命令时,代码 HM-B.ST 中的连字符被解释为减号.我试图将 xts 对象重命名为其他名称,但没有成功.有人知道解决方案吗?

When executing the following commands the hyphen in the ticker HM-B.ST is interpreted as a minus sign. I have tried to rename the xts object to something else but have not succeeded. Does anybody know a solution for this?

>library(quantmod)
>getSymbols("HM-B.ST")
>chartSeries(HM-B.ST)
Error in inherits(x, "xts") : object 'HM' not found

推荐答案

处理这个问题最简洁的方法是不依赖 getSymbols() 的默认自动分配行为,而是分配时间序列反对您自己选择的更标准的名称.例如:

The cleanest way to deal with this is to not rely on getSymbols()' default auto-assignment behavior, and instead assign the time series object to a more standard name of your own choosing. For example:

HM.B.ST <- getSymbols("HM-B.ST", auto.assign=FALSE) # h.t. Joshua Ulrich
chartSeries(HM.B.ST)

<小时>

如果由于某种原因您确实希望时间序列保留其默认的带连字符的名称,您可以通过以下方式访问它:


If for some reason you do want the time-series to retain its by-default hyphenated name, you can access it by doing:

chartSeries(`HM-B.ST`)

它起作用的原因是反引号向R解析器发出信号,它们之间的字符将被解析为单个名称(又名符号),而不是由减法运算符分隔的两个名称.

The reason it works is that the backticks signal to the R parser that the characters between them are to be parsed as a single name (aka symbol), not as two names separated by the subtraction operator.

要一劳永逸地解决这个问题,请尝试以下操作:

To drive that point home once and for all, try something like the following:

assign("a really stupidly constructed name!*&^", 5)
`a really stupidly constructed name!*&^`
# [1] 5

这篇关于如何在 Quantmod 中处理雅虎财经股票代码中的连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆