向ddply传递函数参数

时间:2021-09-17 18:48:17

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

如果我直接以长度()指定“物种”,我就得到了我要寻找的

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. this works insomuch as feature is found (without here() we get an error), however it doesn't return the length as expected.

最近的问题描述了类似的问题,建议在这里使用()在ddply中使用总结()函数,以便告诉ddply在哪里查找变量。由于发现了特性(没有这里(),我们会得到一个错误),所以这是可行的,但是它并没有返回预期的长度。

Any ideas?

什么好主意吗?

1 个解决方案

#1


2  

You are passign a string name "Species" to a ddply function. So you should get it's value inside. Then ddply recognize column name

将字符串名称“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")

#1


2  

You are passign a string name "Species" to a ddply function. So you should get it's value inside. Then ddply recognize column name

将字符串名称“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")