Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

names and getSymbols #420

Open
ggrothendieck opened this issue Aug 4, 2024 · 1 comment
Open

names and getSymbols #420

ggrothendieck opened this issue Aug 4, 2024 · 1 comment

Comments

@ggrothendieck
Copy link

ggrothendieck commented Aug 4, 2024

Consider this code giving the ugly names on stocks.

tickers <- c("^GSPC", "000001.SS")
s <- tickers |> getSymbols()
stocks <- s |> mget() |> Map(f = Ad) |> do.call(what = "merge")

print(s)
## [1] "GSPC"      "000001.SS"

print(names(stocks))
## [1] "GSPC.Adjusted"       "X000001.SS.Adjusted"

Suppose we would prefer s to define the names here. We can arrange for that by adding a setNames to the end of the pipeline but the problem with that is that it uses s twice and that is bad form. A pipeline should have a single input on the very left hand side.

stocks <- s |> mget() |> Map(f = Ad) |> do.call(what = "merge") |> setNames(s)

This can be addressed by defining mergeList as follows:

mergeList <- function(x) do.call("merge", x) |> setNames(names(x))
stocks <- tickers |> getSymbols() |> mget(envir=.GlobalEnv) |> Map(f=Ad) |> mergeList()
print(names(stocks))
## [1] "GSPC"      "000001.SS"

but it required more thought than I had expected to get to this.

A related problem is that we might want to specify our own names for the tickers and it would be nice if getSymbols used the names on tickers if it were named. In the code below the output would be c("sp500", "ssec")

tickers <- c(sp500 = "^GSPC", ssec = " "000001.SS")
getSymbols(tickers)

@joshuaulrich
Copy link
Owner

Thanks for the report and suggestion! Providing names on the tickers vector is one of the options I thought of for rfimport. I'd appreciate your thoughts on any of the design ideas in that readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants