如何获取一个字符串,然后调用名称=该字符串的数据框?

发布于 2024-10-07 06:23:41 字数 561 浏览 1 评论 0原文

以下代码返回一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-10-14 06:23:42

tail(get(x)) 有效吗?

Will tail(get(x)) work?

鸢与 2024-10-14 06:23:42

您是否只是忽略了 getSymbols() 有一个选项 auto.assign 的事实?

因此,您可能需要这样:

R> library(quantmod)
R> silly <- function(sym) {
+     x <- getSymbols(sym, auto.assign=FALSE)
+     tail(x)
+ }
R> silly("IBM")
           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2010-12-03   144.25   145.68  144.25    145.38    3710600       145.38
2010-12-06   144.54   145.87  144.52    144.99    3321800       144.99
2010-12-07   146.02   146.30  143.87    144.02    4828600       144.02
2010-12-08   144.35   145.65  143.84    144.98    4961400       144.98
2010-12-09   145.94   145.94  143.52    144.30    4405300       144.30
2010-12-10   144.88   144.95  143.73    144.82    3503800       144.82
R> silly("C")
           C.Open C.High C.Low C.Close   C.Volume C.Adjusted
2010-12-03   4.38   4.46  4.35    4.45  360277300       4.45
2010-12-06   4.45   4.50  4.43    4.45  445170000       4.45
2010-12-07   4.55   4.65  4.54    4.62 3265796000       4.62
2010-12-08   4.61   4.64  4.55    4.64  913820900       4.64
2010-12-09   4.68   4.71  4.64    4.69  731119000       4.69
2010-12-10   4.70   4.77  4.66    4.77  763156100       4.77
R> 

getSymbols()“我会将其作为新变量粘贴到您的环境中”的默认行为或多或少是一个设计缺陷,据我记得,也被认为是这样的。

因此,可以通过 auto.assign 来改变行为。

Aren't you simply overlooking the fact that getSymbols() has an option auto.assign ?

So you may want this instead:

R> library(quantmod)
R> silly <- function(sym) {
+     x <- getSymbols(sym, auto.assign=FALSE)
+     tail(x)
+ }
R> silly("IBM")
           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2010-12-03   144.25   145.68  144.25    145.38    3710600       145.38
2010-12-06   144.54   145.87  144.52    144.99    3321800       144.99
2010-12-07   146.02   146.30  143.87    144.02    4828600       144.02
2010-12-08   144.35   145.65  143.84    144.98    4961400       144.98
2010-12-09   145.94   145.94  143.52    144.30    4405300       144.30
2010-12-10   144.88   144.95  143.73    144.82    3503800       144.82
R> silly("C")
           C.Open C.High C.Low C.Close   C.Volume C.Adjusted
2010-12-03   4.38   4.46  4.35    4.45  360277300       4.45
2010-12-06   4.45   4.50  4.43    4.45  445170000       4.45
2010-12-07   4.55   4.65  4.54    4.62 3265796000       4.62
2010-12-08   4.61   4.64  4.55    4.64  913820900       4.64
2010-12-09   4.68   4.71  4.64    4.69  731119000       4.69
2010-12-10   4.70   4.77  4.66    4.77  763156100       4.77
R> 

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.

烟雨扶苏 2024-10-14 06:23:42

这很棘手,因为 quantmod 创建一个数据框,其名称与您分配给 x 的字符串相同。因此,首先您需要一个字符串值,然后您将通过名称 x 调用数据框。这正是 do.call() 的用处。

IAmMoney <- function(x) {
    require("quantmod")
    getSymbols(x)
    tail(get(x))
    # changed to get(x) per Ahala's input below. 
    # if you had many params you were passing, do.call()
    # might make more sense
}

IAmMoney("GLD")

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.

IAmMoney <- function(x) {
    require("quantmod")
    getSymbols(x)
    tail(get(x))
    # changed to get(x) per Ahala's input below. 
    # if you had many params you were passing, do.call()
    # might make more sense
}

IAmMoney("GLD")

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文