I a nutshell I would like to run the following minimal piece of code:
简单地说,我想运行以下最小的代码段:
library(plyr)
surround = function(my.df, my.var, my.val, my.method) {
ddply(my.df, my.var, summarize, value = my.method(as.name(my.val)))
}
my.df = data.frame(group = rep(letters[1:4], times = 25),
x = rnorm(100))
surround(my.df, "group", "x", mean)
However, this leads to Error: could not find function "my.method"
. I do realize that this is a scoping issue and I should be using eval
or substitute
but I can't figure it out.
但是,这会导致错误:无法找到函数“my.method”。我确实意识到这是一个范围问题,我应该使用eval或代换,但我搞不清楚。
1 个解决方案
#1
4
If you use a customised function instead of summarise, it would work.
如果您使用定制的函数而不是摘要,它将会工作。
surround <- function(my.df, my.var, my.val, my.method) {
ddply(my.df, my.var, function(x) c(value = my.method(x[[my.val]])))
}
my.df <- data.frame(group=rep(letters[1:4], times=25),
x=rnorm(100))
surround(my.df, "group", "x", mean)
#1
4
If you use a customised function instead of summarise, it would work.
如果您使用定制的函数而不是摘要,它将会工作。
surround <- function(my.df, my.var, my.val, my.method) {
ddply(my.df, my.var, function(x) c(value = my.method(x[[my.val]])))
}
my.df <- data.frame(group=rep(letters[1:4], times=25),
x=rnorm(100))
surround(my.df, "group", "x", mean)