如何消除R函数中参数的引号? [英] How can I eliminate quote marks around parameters in R function?

查看:1302
本文介绍了如何消除R函数中参数的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  teetor<  -  function(x,y)这是一个R函数的前几行: {

require(quantmod)
require(tseries)

alpha< - getSymbols(x,auto.assign = FALSE)
bravo< - getSymbols(y,auto.assign = FALSE)

t< - as.data.frame(merge(alpha,bravo))

#.. 。<钻孔单位根计量经济代码>

}

当我传递两个股票代码作为函数参数时,我需要用引号将它们括起来:

  teetor(GLD,GDX)



我希望能够简单地输入:

  teetor(GLD,GDX)


解决方案

这样做的方法,但通常我不会建议它。

通常调用不带引号的内容意味着对象本身位于搜索路径中。一种不分配它的方法是使用和()函数。



你可以得到名字没有它的东西实际上是由 deparse(substitute(...))存在的:

 > blah<  -  function(a){
deparse(substitute(a))
}
> blah(foo)
[1]foo
> foo
错误:找不到对象'foo'

原则上,在 teetor 函数中使用 deparse(substitute(...))作为上面的例子,而不是传入名。

Here are the first few lines of an R function that works:

teetor <- function(x,y) {

require("quantmod")
require("tseries")

alpha <- getSymbols(x, auto.assign=FALSE)
bravo <- getSymbols(y, auto.assign=FALSE)

t     <- as.data.frame(merge(alpha, bravo))

# ... <boring unit root econometric code>

}

When I pass two stock symbols as function parameters, I need to enclose them with quotes:

teetor("GLD", "GDX")

I want to be able to simply type:

teetor(GLD, GDX)

解决方案

There are several ways of doing this, but generally I wouldn't advise it.

Typically calling something without quotes like that means that the object itself is in the search path. One way to do this without assigning it is to use the with() function.

You can get the name of something without having it actually exist by deparse(substitute(...)):

> blah <- function(a) {
    deparse(substitute(a))
  }
> blah(foo) 
[1] "foo"
> foo 
Error: object 'foo' not found

So in principle you can get the names using deparse(substitute(...)) as in the above example in your teetor function instead of passing in the names.

这篇关于如何消除R函数中参数的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆