0

I'm an R beginner and am using dplyr and trying to incorporate a custom function into my R script.

I have a data frame called ss, I want to apply filters to it, but I want to separate out the filters into a separate function so it can be shared with other scripts.

Code example:

#my function
apply_filters <- function(d){
    #Applying filter to d
    dd <- d %>% filter([some filter])
    return(dd)
}

ss2 <- ss %>%
    do(apply_filters(.)) %>%
    summarize()

I'm hoping to be able to send the current state of ss2 to the apply_filters function and get the resultant data back so I can summarize it.

This doesn't work as I get an error:

Error: invalid 'type' (character) of argument

Am I missing something?

user791793
  • 413
  • 1
  • 6
  • 19
  • 1
    Pseudocode is not nearly as helpful as a reproducible example http://stackoverflow.com/help/mcve – Frank Aug 05 '16 at 16:52
  • 1
    @Steven The OP's notion of filters is pretty vague, but I'm guessing they can avoid using `do()`. For example `apply_filters = . %>% filter(mpg > 20)` can be used directly in the chain `mtcars %>% apply_filters` – Frank Aug 05 '16 at 16:57

0 Answers0