简体   繁体   English

将函数参数传递给ddply

[英]Passing a function argument to ddply

I know there are various similar questions out there and as such I aplogize for the repetition. 我知道那里有各种类似的问题,因此我为重复而烦恼。 That said, while I have found useful information on this topic, nothing I've attempted seems to work. 也就是说,虽然我已经找到了关于这个主题的有用信息,但我所尝试的一切似乎都没有用。

In short, I'm using ddply inside a function, and attempting to pass an argument from the function to a function in ddply. 简而言之,我在函数内部使用ddply,并尝试将函数中的参数传递给ddply中的函数。

A simplified example using the iris dataset 使用iris数据集的简化示例

IG_test <-function(data, feature){
  dd<-ddply(data, feature, here(summarise), N=length(feature))
  return(dd)
}

IG_test(iris, "Species")

This should return the number of records for every Species, but rather returns 1 in each case. 这应该返回每个物种的记录数,而是在每种情况下返回1。

If I specify "Species" directly in length() , I get what I'm looking for 如果我直接在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

The most recent questions describing similar problems suggest using here() for the summarize() function in ddply, in order to tell ddply where to look for the variable. 描述类似问题的最新问题建议在ddply中使用here()作为summarize()函数,以便告诉ddply在哪里查找变量。 this works insomuch as feature is found (without here() we get an error), however it doesn't return the length as expected. 这是因为找到了feature (没有here()我们得到一个错误),但它没有按预期返回长度。

Any ideas? 有任何想法吗?

You are passign a string name "Species" to a ddply function. 您将字符串名称“Species”传递给ddply函数。 So you should get it's value inside. 所以你应该在里面得到它的价值。 Then ddply recognize column name 然后ddply识别列名

library(plyr)
IG_test <-function(data, feature){
  dd<-ddply(data, feature, here(summarise), N=length(get(feature)))
  return(dd)
}

IG_test(iris, "Species")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM