将对象传递到用户定义的功能中
我想创建一个用户定义的函数,以在变量列表上运行Coxph。这个公式运行良好:
summary(coxph(
Surv(var1, factor(var1)) ~ var3,
data = df_wts))
但是,如果我尝试将其转换为函数,我会继续得到“未找到的对象”警告:
cox_fun <- function(x, y) {
summary(coxph(
Surv(x, factor(y)) ~ var3,
data = df_wts))
尝试了所有内容,Enquo(x),{{}},[[]],! ,as.name()等。
关于为什么会发生这种情况的任何解释?环境?谢谢!
I would like to create a user defined function to run coxph over a list of variables. This formula runs just fine:
summary(coxph(
Surv(var1, factor(var1)) ~ var3,
data = df_wts))
But then if I try to turn it into a function, I keep getting an "object not found" warning:
cox_fun <- function(x, y) {
summary(coxph(
Surv(x, factor(y)) ~ var3,
data = df_wts))
Have tried everything, enquo(x), {{}}, [[]], !!, as.name(), etc.
Any explanation for why this is happening? Environment? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的
var3
似乎没有定义。您的var1
是您的第二个代码x
和y
有点奇怪,最后您应该用} 。尝试此代码:
First your
var3
seems not defined. Yourvar1
is your second codex
andy
which is a bit strange and finally you should end the function with}
. Try this code:弄清楚了。主要问题是(我认为)环境没有解决。我通过添加对数据帧本身的调用来修复它。不确定这是否有用,但是以下是更新的代码
添加了一些额外的内容:
我想只输入一个值,所以我命名了具有粘贴函数的变量
,然后我添加了数据库来确保实际处理过的cover() 然后正确地
将功能应用于我的变量列表上。干杯!
figured this out. Main issue was (I think) that the environment wasn't carrying over. I fixed it by adding in calls to the dataframe itself. Not sure if this is helpful, but here's the updated code
A few extra things added:
I wanted to just input a single value, so I named variables with a paste function
Then I added that with the database to make sure Surv() actually processed it properly
Then a quick function to apply the function over my list of variables. Cheers!