0

I have a tbl_df data set called ds, and I want to write a function so that I can filter a column based on some String value. Here is my attempt:

myCol <- as.name(names(ds)[5]) # define which col to pass to the function

 myFunction <- function(ds, myCol,  myString="XXXX" ){    
 myQuant <- ds %>%
       filter(myCol  %in%  myString )%>%
       group_by(x)%>%
       summarize(count = n())   
 return(myQuant)
}  

This produces the following error:
Error during wrapup: 'match' requires vector arguments

If extract the filtering block outside the function and pass the arguments manually, it works fine.

I guess all what the filter function requires is a column name, so why doesn't like it this way?

aosmith
  • 34,856
  • 9
  • 84
  • 118
A.M.A
  • 1
  • 3
  • 1
    `myString` isn't in the argument of your function. Did you intend for myString = "XXXX"? Also, I believe the `%in%` operator requires a vector or list on right hand side. Wrap your search string in `c( )` function. – misspelled Sep 04 '15 at 11:07
  • I have updated it now. – A.M.A Sep 04 '15 at 11:21
  • Still, myString should be a vector when you use %in%, otherwise you should use == – Wannes Rosiers Sep 04 '15 at 11:56
  • what is your input and what do you want as desired output? – Colonel Beauvel Sep 04 '15 at 12:28
  • the input is a data frame (tbl_df) and the output is a count of elements in each group. I am using verbs of dplyr to perform the filtering and and grouping and the summarizing – A.M.A Sep 04 '15 at 15:12
  • 1
    I think you are going to need to use `filter_` and `lazyeval::interp` much like in [this answer](http://stackoverflow.com/a/26509961/2461552). Consider adding an example dataset to make this question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – aosmith Sep 04 '15 at 17:36

0 Answers0