0

This question will be bountied by me eventually if no one gives a satisfactory answer.

I have the following code in R

library(dplyr)
Data <- iris %>% filter(Petal.Length > 5)

The filter function calls upon the iris object and subsets the data by the Petal.Length variable.

What I want is to be able to pass a string through the filter function such that something like this would give the same result.

Data <- iris %>% filter(get("Sepal.Length") > 5)

The above code gives an error though

Error: object 'Sepal.Length' not found

If it's of any benefit for context, get works with the select function within dplyr. For example...

Data <- iris %>% select(Sepal.Length)

Data1 <- iris %>% select(get("Sepal.Length"))

give the same results.

Is there any way to make it such that the two filter statements, where one could pass a string through the latter one, would give the same output?

InfiniteFlash
  • 1,038
  • 1
  • 10
  • 22
  • 3
    There's a whole [vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html) in the dplyr docs that deals with this, as well as I'm sure numerous duplicates that someone will dig up shortly here. If you look, most of those functions have alternate version like `filter_` and `select_` for precisely this purpose. – joran Oct 07 '16 at 18:36
  • 2
    `iris %>% filter_(~Petal.Length > 5)` or `iris %>% filter_("Petal.Length > 5")`, but read the Vignette and look at SO questions on Standard vs. Non-Standard evaluation in `dplyr`. – eipi10 Oct 07 '16 at 18:39
  • 2
    ...[here](http://stackoverflow.com/a/27197858/324364) is one example. – joran Oct 07 '16 at 18:42
  • Okay, thanks. I will have to read that. This question has been nagging me for a while. I wasn't aware of the `filter_`, `~var` functions. – InfiniteFlash Oct 07 '16 at 18:44
  • 1
    Some more examples [here](http://justanotherdatablog.blogspot.com/2015/03/dplyr-use-cases-non-interactive-mode.html). – eipi10 Oct 07 '16 at 18:45
  • I can't quite think of any examples off the top of my head, but it seems rather coincidental that this would be available through `dplyr`. I guess for functions outside of dplyr, it would depend on the package or function themselves. Yes, this is a duplicate as `Coatless` mentions. – InfiniteFlash Oct 07 '16 at 18:53

0 Answers0