6

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

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

1 回答 1

7

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`)

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
于 2013-10-12T18:08:17.000 回答