在函数中使用带引号的变量并将其裸露 [英] Taking quoted variable in a function and making it bare

查看:42
本文介绍了在函数中使用带引号的变量并将其裸露的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它从R中的estimatr包中调用lm_robust函数.我希望能够指定一个变量,以在该变量上集群标准错误,但是lm_robust函数仅允许在lm_robust的集群中使用裸露的(未加引号的)变量名.选项,而我的函数需要输入的内容是用引号引起来的变量名.

I have a function that calls the lm_robust function from the estimatr package in R. I want to be able to specify a variable on which to cluster standard errors, but the lm_robust function only allows bare (unquoted) variable names in lm_robust's cluster option while my function needs the input to be a quoted variable name.

如何获取输入到函数中的变量(例如"cl")并将其转换为未加引号的变量(例如cl)?

How do I take a variable that is input into a function (such as "cl") and turn it into a unquoted variable (such as cl)?

推荐答案

do.call 中使用 as.name :

library(estimatr)
example(lm_robust)
## ... snip ...

clname <- "clusterID"
do.call("lm_robust", list(y ~ x + z, data = quote(dat), weights = quote(w),
   clusters = as.name(clname)))

给予:

              Estimate Std. Error    t value     Pr(>|t|)  CI Lower  CI Upper
(Intercept)  3.4261621  0.2009692 17.0481986 1.332761e-05  2.908643  3.943681
x           -0.6734741  0.1351184 -4.9843254 4.300590e-03 -1.022076 -0.324872
z            0.5850340  0.9436175  0.6199907 5.566933e-01 -1.689652  2.859720
                  DF
(Intercept) 4.970885
x           4.940162
z           6.396615

这篇关于在函数中使用带引号的变量并将其裸露的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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