0

Possible Duplicate:
R: getting a function name as a string

1) I have a variable that stores data in the first column of a text file (stock ticker symbols)

tickers <- read.csv("stocks.txt", header=FALSE, sep=",")
tickers <- tickers[1]

2) For each ticker I run: getSymbols(tickers, from=startdate, to=enddate)

getSymbols is from the quantmod package

The result of calling 'getSymbols' is a series of xts objects that have the same name as the names in the tickers variable.

Now what I want to do is determine the date of the first element in each of the xts objects. Since each object has the same name as the ticker symbol associated with it in tickers variable I thought I could just do the following in a for loop where i is the index iteration:

min(index(tickers[i]))

However this does not work because tickers[1] returns a character name and not an object which index() is expecting. The problem is the character returned by tickers[1] is the name of the xts object created by getSymbols.

I appreciate the help. Thank you

Community
  • 1
  • 1
codingknob
  • 11,108
  • 25
  • 89
  • 126
  • Please use the search function. The 3rd result of the first search I tried ("[r] object name") answers your question. And someone asked [a similar question](http://stackoverflow.com/questions/5796508/loop-through-ls-or-objects) only 4 hours ago. – Joshua Ulrich Apr 27 '11 at 01:26
  • 1
    just try `get(tickers[i]` in place of `tickers[i]`. that should do the trick as `get` returns the object corresponding to the name in `tickers[i]`. – Ramnath Apr 27 '11 at 01:28
  • Thank you gentlemen. I searched using the wrong key words hence the frustration. Thank you for bringing this to my attention. I will spend more time searching the next time around. I greatly appreciate your help. – codingknob Apr 27 '11 at 01:43
  • @Joshua @Ramnath can someone write the `get()` solution up as an answer or vote to close so we can wrap this one up? – Gavin Simpson Apr 27 '11 at 08:39

1 Answers1

1

getSymbols is rather odd in how it assigns things. What you want is a list of time series, rather than a load of individual variables. Coincidentally, (cue shameless self-promotion) I wrote about how to create this last week.

As an alternative, you can use get, as Ramnath suggested.

sapply(tickers, function(x) min(index(get(x))))
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360