Search code examples
r

Import Financial Data for index ticker that starts with a numerical no. into R Programming


R Programming

I have no problem importing financial data from yahoo/FRED with index ticker such as TSLA, AAPL but when a particular stock is named after a numerical such as 1475.T

The error returns is Error: unexpected symbol in "print(1475.T"

Any expert out there who can point out what is the mistake? Otherwise, no error occurs for other stock ticker that doesn't consist of any numerical, such as AAPL, TSLA.

Thank you


Solution

  • To increase chances of getting an answer, post a reproducible example. A reproducible example consists of a minimal dataset similar to one you're using (or use dput() on your data and post the output) and an example of code that you have that isn't working.

    To your post, the reason you get that error is because getSymbols() uses auto.assign = TRUE by default. This means that whatever data you download gets saved to the environment as its ticker symbol. However, R objects are not allowed to start with a number, so these objects are saved using backticks like so:

    `1475.T`
    

    So if you want to access the object, call it using backticks

    View(`1475.T`)
    print(`1475.T`)
    

    Or preferrably, set auto.assign to FALSE and save the result to a variable.

    library(quantmod)
    iShares.Core.ETF <- getSymbols("1475.T", auto.assign = FALSE)