0

I want to define a simple function that will filter a data frame, similar to something you might do in SPSS with the "Select Cases" menu option.

Here is what I would type in the console to filter it using the subset function if I wanted to only keep cases where q3a is 1: subset(df, q3a==1). This will run with no problem and returns a data frame including only those cases where q3a is 1.

However, when I use the following function, I get an error:

filter <- function(frame, var, val) {
    newFrame <- subset(frame, var==val)
    return(newFrame)
}

When I try to run filter(df, q3a, 1), R throws me the following error: Error in eval(expr, envir, enclos) : object 'q3a' not found.

I can't see what the problem is here. It seems like the function should just directly pass the arguments to the subset function, but clearly that's not happening.

nrussell
  • 18,382
  • 4
  • 47
  • 60
Matthew Chatham
  • 320
  • 1
  • 9
  • 3
    just for interest http://stackoverflow.com/questions/9860090/why-is-better-than-subset – user20650 Jan 05 '17 at 01:29
  • 1
    Are you aware of the function [`filter`](https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html) from `dplyr` which does what you want? It's advisable to use functions from established packages where possible. – Hugh Jan 05 '17 at 02:41
  • 1
    `filter(df, df$q3a, 1)`. – xtluo Jan 05 '17 at 03:38
  • 1
    @user20650 Thanks! I will use indices from now on. – Matthew Chatham Jan 06 '17 at 01:30
  • @Hugh, I was not aware, thank you for the information. However, I was using this more as a learning example so I am still interested in understanding why the function I wrote wasn't working. – Matthew Chatham Jan 06 '17 at 01:31

1 Answers1

0

Most built-in R functions will take an unquoted argument like q3a and make it behave like a string.

To achieve the same functionality, try this:

filter <- function(frame, var, val) {
    return(frame[eval(substitute(var), frame)==val, ])
}

Notice I'm using a different approach rather than subset, which uses non-standard evaluation and can make things very complicated.

airstrike
  • 2,270
  • 1
  • 25
  • 26