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.
A simplified example using the iris
dataset
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.
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.
Any ideas?
You are passign a string name "Species" to a ddply function. So you should get it's value inside. Then ddply recognize column name
library(plyr)
IG_test <-function(data, feature){
dd<-ddply(data, feature, here(summarise), N=length(get(feature)))
return(dd)
}
IG_test(iris, "Species")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.