开发者

How to paste a variable name into R `plot`

开发者 https://www.devze.com 2023-04-03 15:32 出处:网络
This seems like it should be pretty obvious, but I\'ve tried substitute, bquote, expression, paste, and cat, with similar results (failure).

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

require(quantm开发者_Python百科od)
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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消