在R中使用quantmod包修复错误时遇到问题:继承错误(x," xts"):对象' M'没找到电话

时间:2017-04-15 15:17:11

标签: r quantmod

下面是我的代码。我想要做的是下载股票和情节图表的多个符号,但我收到此错误:

继承错误(x," xts"):对象' M'未找到电话:... EVAL-> chartSeries - > try.xts - > is.xts - >继承

library(quantmod)
stocks <- c("INFY.NS","HINDALCO.NS","TCS.NS","TATASTEEL.NS","NESTLEIND.NS",
  "TECHM.NS","HCLTECH.NS","HDFCBANK.NS","JETAIRWAYS.NS","BLUEDART.NS",
  "AUROPHARMA.NS","APOLLOHOSP.NS", "IOC.NS", "HINDPETRO.NS","BHEL.NS",
  "NTPC.NS", "HINDZINC.NS", "M&M.NS", "RELIANCE.NS","ONGC.NS", "ORIENTBANK.NS",
  "MARUTI.NS", "ASHOKLEY.NS", "ASIANPAINT.NS", "DABUR.NS", "DRREDDY.NS",
  "BHARTIARTL.NS", "BATAINDIA.NS", "LTI.NS")
getSymbols(stocks,from="2017-01-01")
chartSeries(INFY.NS)  
chartSeries(HINDALCO.NS)
chartSeries(TCS.NS)
chartSeries(TATASTEEL.NS)
chartSeries(NESTLEIND.NS)
chartSeries(TECHM.NS)
chartSeries(HCLTECH.NS)
chartSeries(HDFCBANK.NS)
chartSeries(JETAIRWAYS.NS)
chartSeries(BLUEDART.NS)
chartSeries(AUROPHARMA.NS)
chartSeries(APOLLOHOSP.NS)
chartSeries(IOC.NS) 
chartSeries(HINDPETRO.NS) 
chartSeries(BHEL.NS) 
chartSeries(NTPC.NS)
chartSeries(HINDZINC.NS)
chartSeries(M&M.NS)
chartSeries(RELIANCE.NS)
chartSeries(ONGC.NS)
chartSeries(ORIENTBANK.NS)
chartSeries(MARUTI.NS)
chartSeries(ASHOKLEY.NS)
chartSeries(ASIANPAINT.NS)
chartSeries(DABUR.NS)
chartSeries(DRREDDY.NS)
chartSeries(BHARTIARTL.NS)
chartSeries(BATAINDIA.NS)
chartSeries(LTI.NS)

1 个答案:

答案 0 :(得分:0)

问题是M&M.NS不是语法上有效的R名称(&具有特殊含义)。

library(quantmod)
getSymbols("M&M.NS")
chartSeries(M&M.NS)    # error
chartSeries(`M&M.NS`)  # use backticks for names that aren't syntactically valid

或者您可以使用setSymbolLookup()将符号映射到语法上有效的名称。然后,您可以在getSymbols()chartSeries()来电中使用语法上有效的自动收报机。

setSymbolLookup(M.AMP.M.NS = list(name = "M&M.NS"))
getSymbols("M.AMP.M.NS")
chartSeries(M.AMP.M.NS)