Passing an argument to ddply function
I know there are various similar questions, and as such I plan to repeat. However, while I found some useful information on this topic, nothing I tried seems to work.
Long story short, I am using ddply inside a function and am trying to pass an argument from the function to the ddply function.
Simplified example using 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 each view, but returns 1 in each case.
If I specify "Views" directly in length()
, I get what I am 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 addressing similar problems suggest using a here()
function summarize()
in ddply to tell ddply where to look for a variable. this works the same as feature
found (without here()
we get the error), however it doesn't return the length as expected.
Any ideas?
source to share
You enter the name of the line "Views" into the ddply function. Therefore, you must get this value internally. Then ddply find out the 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")
source to share