-
-
Notifications
You must be signed in to change notification settings - Fork 228
Open
Description
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels