如何获取一个字符串,然后调用名称=该字符串的数据框?
以下代码返回一个名为“GLD”的字符串。
CatItUp <- function(x){
print(x)
}
CatItUp("GLD")
此代码返回 GLD 价格的尾部。但显然,因为我已将 GLD 硬编码到该函数中。
IAmMoney <- function(x) {
require("quantmod")
getSymbols("GLD")
tail(GLD)
}
IAmMoney("GLD")
这不会像硬编码版本那样返回价格,而是像上面的 CatItUp() 示例一样返回“GLD”字符串。我不知道为什么。
IAmMoney <- function(x) {
require("quantmod")
getSymbols("x")
tail(x)
}
IAmMoney("GLD")
如何将“GLD”传递给 IAmMoney() 函数内的 quantmod::getSymbols 函数?
The following code returns a string called "GLD".
CatItUp <- function(x){
print(x)
}
CatItUp("GLD")
This code returns the tail of GLD prices. But obviously, because I've hard-coded GLD into the function.
IAmMoney <- function(x) {
require("quantmod")
getSymbols("GLD")
tail(GLD)
}
IAmMoney("GLD")
This does not return prices like the hard-coded version, but the "GLD" string like the CatItUp() example above. I don't know why.
IAmMoney <- function(x) {
require("quantmod")
getSymbols("x")
tail(x)
}
IAmMoney("GLD")
How can you pass 'GLD' to the quantmod::getSymbols function, inside the IAmMoney() function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
tail(get(x))
有效吗?Will
tail(get(x))
work?您是否只是忽略了
getSymbols()
有一个选项auto.assign
的事实?因此,您可能需要这样:
getSymbols()
“我会将其作为新变量粘贴到您的环境中”的默认行为或多或少是一个设计缺陷,据我记得,也被认为是这样的。因此,可以通过 auto.assign 来改变行为。
Aren't you simply overlooking the fact that
getSymbols()
has an optionauto.assign
?So you may want this instead:
getSymbols()
default behaviour of "I will stick it into your environment as a new variable" is more or less a design flaw and, as I recall, recognised as such.And hence the behaviour can be altered by
auto.assign
.这很棘手,因为 quantmod 创建一个数据框,其名称与您分配给 x 的字符串相同。因此,首先您需要一个字符串值,然后您将通过名称 x 调用数据框。这正是 do.call() 的用处。
Dirk 指出,使用
auto.assign=FALSE
参数意味着您可以简单地执行此操作:tail(getSymbols("GLD", auto.assign=FALSE))
It's tricky because quantmod creates a data frame who's name is the same as the string you assign to x. So at first you need a string value then later you are calling a data frame by the name of x. This is exactly what do.call() is helpful for.
Dirk pointed out that the use of the
auto.assign=FALSE
argument which means you can simply do this instead:tail(getSymbols("GLD", auto.assign=FALSE))