需要在子应用内访问父应用的变量,而不需要全局范围
让我再试一次,我将省略确切的数据/示例,只介绍我需要完成的任务。
我需要在 data.frame 的行上应用一个函数,这很简单。然后我需要使用传递给它的 data.frame 派生该函数中的一些变量。最后,我想在 data.frame 的子集上应用一个新函数,并在新函数中使用派生变量。
有人可以告诉我执行此操作的最佳实践方法,而不是全局范围限定每个变量(var1、var2)吗?
cpt <- a.data.frame
query.db <- function(another.data.frame){
var1 <- some.values
var2 <- some.other.values
apply(cpt[var1,], 1, calc.enrichment) #calc.enrichment needs to access var1, var2!
}
我尝试将 calc.enrichment 函数编写为用户定义的函数,而不是在范围之外,但我的参数列表(var1、var2)未被识别。感谢您的任何帮助。
Let me try this again, I'm going to leave out the exact data/example and just walk through what I need to accomplish.
I need to apply a function over the rows of a data.frame, that is easy. Then I need to derive some variables within that function using the data.frame that was passed to it. Finally, I'd like to apply a new function over a subset of the data.frame and use the derived variables in the new function.
Can someone please tell me the best practice way to do this rather than globally scoping each of my variables (var1, var2)?
cpt <- a.data.frame
query.db <- function(another.data.frame){
var1 <- some.values
var2 <- some.other.values
apply(cpt[var1,], 1, calc.enrichment) #calc.enrichment needs to access var1, var2!
}
I tried writing the calc.enrichment function as a user-defined function rather than outside of the scope, but my list of arguments (var1, var2) weren't being recognized. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个愚蠢的例子对我有用,似乎可以解决您所追求的问题。像您一样,我们使用
var1
对 apply 函数中使用的 data.frame 的列进行索引。var2
只是传递给它的 data.frame 第一列的标准差。我猜你的真实例子做了一些更有用的事情。This silly example works for me and seems to address what you are after. We use
var1
to index into the columns of the data.frame used in the apply function as you did.var2
is just the standard deviation of the first column of the data.frame passed to it. I'm guessing your real example does something a tad bit more useful.