3

This seems like it should be pretty obvious, but I've tried substitute, bquote, expression, paste, and cat, with similar results (failure).

require(quantmod)
getSymbols("SAM")
thing = "SAM"
plot(SAM)      #this works fine
plot(thing)    #this does not

Encasing thing in xts(thing) and so on doesn't work either.

Community
  • 1
  • 1
isomorphismes
  • 8,233
  • 9
  • 59
  • 70

1 Answers1

4

How about this:

plot(get(thing))  

Running thing = "SAM" simply assigns the character "SAM" to a variable named thing. R has no way to know (without you telling it) that you want it to connect the value of the character vector thing to a particular object in the environment (i.e. SAM). So get does the trick here.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Addendum: I'm actually trying to `plot(SAM[,6])` but I thought the way I asked the question would be simpler. I tried `plot(get(cat(thing` and `plot(cat(get(thing` concatenated with `"[,6]", sep="")))` -- but neither that nor setting `thing = "SAM[,6]"` and `plot(get(thing))` works. (`plot(SAM[,6]` works.) – isomorphismes Sep 06 '11 at 21:26
  • 1
    @Jack Maney I wasn't allowed to accept right away (there's a time limit on the checkmark button). – isomorphismes Sep 06 '11 at 21:28
  • 1
    @LaoTzu I think maybe what you want, then, is `plot(get(thing)[,6])`. `get` retrieves the object, _then_ you can apply `[` subsetting. – joran Sep 06 '11 at 21:30
  • 1
    And note that you can replace the 6 with the character `"SAM.Adjusted"` if you like. – joran Sep 06 '11 at 21:31
  • Thanks again! That was right too. I don't even want to think about how to get `SAM.adjusted` to wear a variable value after so much work, so I'll stick with the `[,6]` -- that's the same for all `quantmod` tickers. – isomorphismes Sep 06 '11 at 21:34
  • You can also use the `Ad` function in quantmod to get the `SAM.adjusted` column. – Joshua Ulrich Aug 31 '12 at 02:27