【问题标题】:How to paste a variable name into R `plot`如何将变量名称粘贴到 R `plot`
【发布时间】:2011-09-06 21:05:31
【问题描述】:

这似乎应该很明显,但我尝试过substitutebquoteexpressionpastecat,结果相似(失败)。

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

xts(thing) 中包含thing 等也不起作用。

【问题讨论】:

    标签: r


    【解决方案1】:

    这个怎么样:

    plot(get(thing))  
    

    运行thing = "SAM" 只需将字符“SAM”分配给名为thing 的变量。 R 无法知道(没有你告诉它)你希望它把字符向量thing 连接到环境中的特定对象(即SAM)。所以get 就在这里。

    【讨论】:

    • 附录:我实际上正在尝试plot(SAM[,6]),但我认为我提出问题的方式会更简单。我尝试将plot(get(cat(thingplot(cat(get(thing"[,6]", sep=""))) 连接起来——但无论是这样还是设置thing = "SAM[,6]"plot(get(thing)) 都不起作用。 (plot(SAM[,6] 有效。)
    • @Jack Maney 我不能马上接受(复选标记按钮有时间限制)。
    • @LaoTzu 我想也许你想要的是plot(get(thing)[,6])get 检索对象,然后您可以应用 [ 子集。
    • 请注意,如果您愿意,可以将 6 替换为字符 "SAM.Adjusted"
    • 再次感谢!那也是对的。我什至不想考虑如何让SAM.adjusted 在经过这么多工作后佩戴变量值,所以我会坚持使用[,6]——这对所有quantmod 代码都是一样的。
    猜你喜欢
    • 2018-11-11
    • 2019-04-10
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多