Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paste a variable name into R `plot`

Tags:

r

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.

like image 766
isomorphismes Avatar asked Mar 26 '26 00:03

isomorphismes


1 Answers

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.

like image 185
joran Avatar answered Mar 28 '26 17:03

joran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!