sflee
sflee

Reputation: 1719

How to rename the return data from getSymbols?

I would like to ask that can I rename the result data so that I can do:

From

getSymbols("N225", from="1950-01-01")
head(N225)

To

xx = getSymbols("N225", from="1950-01-01")
head(xx)

I come up with this question because I would like to download some data with a symbol name as a number.

data.env <- new.env()
getSymbols("0005.HK", env=data.env)
ls.str(data.env)

0005.HK : An 'xts' object on 2007-01-02/2019-05-23 containing:
  Data: num [1:3058, 1:6] 143 144 145 144 142 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "0005.HK.Open" "0005.HK.High" "0005.HK.Low" "0005.HK.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2019-05-24 23:14:45"

Thank you very much.

Upvotes: 0

Views: 537

Answers (2)

Jared M
Jared M

Reputation: 977

You can do it in a loop similar to how the person above suggested.

prices <- list()
for(i in 1:length(tickers)) {
  getSymbols(tickers[i], adjusted = TRUE, output.size = "full")

  prices[[i]] <- get(tickers[i]) 
}

Upvotes: 1

r2evans
r2evans

Reputation: 160417

From ?getSymbols:

Data is loaded silently without user assignment by default.

Luckily, this suggests (and the arguments support it) the ability to disable this feature:

env               where to create objects. Setting env=NULL is equal
                  to auto.assign=FALSE

auto.assign       should results be loaded to env If FALSE, return
                  results instead. As of 0.4-0, this is the same as
                  setting env=NULL. Defaults to TRUE

where the key statement is "return results instead".

Either of the following should return the data instead of operating in side-effect:

xx <- getSymbols("N225", from="1950-01-01", env=NULL)
xx <- getSymbols("N225", from="1950-01-01", auto.assign=FALSE)

Upvotes: 3

Related Questions