将函数参数传递给 ddply
Passing a function argument to ddply
我知道那里有各种类似的问题,因此我对重复问题深表歉意。也就是说,虽然我找到了关于这个主题的有用信息,但我尝试过的任何事情似乎都没有用。
简而言之,我在函数内部使用 ddply,并试图将参数从函数传递给 ddply 中的函数。
使用 iris
数据集的简化示例
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(feature))
return(dd)
}
IG_test(iris, "Species")
这应该是每个物种的 return 记录数,而不是 return 每种情况下的 1 条记录。
如果我直接在 length()
中指定 "Species",我就会得到我要找的东西
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(Species))
return(dd)
}
IG_test(iris, "Species")
Species N
1 setosa 50
2 versicolor 50
3 virginica 50
描述类似问题的最新问题建议在 ddply 中对 summarize()
函数使用 here()
,以便告诉 ddply 在哪里查找变量。这是有效的,因为 feature
被发现(没有 here()
我们得到一个错误),但是它没有 return 预期的长度。
有什么想法吗?
您正在将字符串名称 "Species" 传递给 ddply 函数。所以你应该在里面得到它的价值。然后ddply识别列名
library(plyr)
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(get(feature)))
return(dd)
}
IG_test(iris, "Species")
我知道那里有各种类似的问题,因此我对重复问题深表歉意。也就是说,虽然我找到了关于这个主题的有用信息,但我尝试过的任何事情似乎都没有用。
简而言之,我在函数内部使用 ddply,并试图将参数从函数传递给 ddply 中的函数。
使用 iris
数据集的简化示例
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(feature))
return(dd)
}
IG_test(iris, "Species")
这应该是每个物种的 return 记录数,而不是 return 每种情况下的 1 条记录。
如果我直接在 length()
中指定 "Species",我就会得到我要找的东西
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(Species))
return(dd)
}
IG_test(iris, "Species")
Species N
1 setosa 50
2 versicolor 50
3 virginica 50
描述类似问题的最新问题建议在 ddply 中对 summarize()
函数使用 here()
,以便告诉 ddply 在哪里查找变量。这是有效的,因为 feature
被发现(没有 here()
我们得到一个错误),但是它没有 return 预期的长度。
有什么想法吗?
您正在将字符串名称 "Species" 传递给 ddply 函数。所以你应该在里面得到它的价值。然后ddply识别列名
library(plyr)
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(get(feature)))
return(dd)
}
IG_test(iris, "Species")