Creating function in R

Hi all, Below is the simple filtering that is done on Iris data set


iris_new <- iris %>% filter(Species == "setosa")

But I am trying to create a function where the functions takes data, category as input and then filters. Example

## df is a dataframe, cat is column on which filtering is applied and cat_val is the categorical values
filter_fun <- function(df, cat, cat_val){
  res <- df %>% filter(cat == "cat_val")
return(res)
}

So when I try running below, I get error. Appreciate if anyone could help me whtat wrong I am doing here

filter_fun(iris, Species, "setosa")

I think that there are two problems with the code, one that is explicit, which is the one that you are getting, and one implicit. In the code that you have:

You get the following error: object 'Species' not found. That's because Species doesn't come on its own, it comes from the iris dataset, so you should have:

filter_fun(iris, iris$Species, "setosa")

But that won't work either, not in the sense that you would get an error, but it won't do what you want. That's because in your function you have cat == "cat_val", so the filter will try to match to specifically "cat_val", so you should change it to cat = cat_val for it to work correctly.

Hope that helps

Thanks. I tried below as you mentioned but still got error

filter_fun <- function(df, cat, cat_val){
  res <- df %>% filter(cat = cat_val)
  return(res)
}
filter_fun(iris, iris$Species, "setosa")

Error: Problem with `filter()` input `..1`.
x Input `..1` is named.
ℹ This usually means that you've used `=` instead of `==`.
ℹ Did you mean `cat == cat_val`?
Run `rlang::last_error()` to see where the error occurred. 

Hi,

use '==' instead of '=' in the function.

1 Like

Yeah, now I had an error in

It should be cat == cat_val.

Always read the error message, it says in there

so it suggests to change = to ==

Thanks Both. I got it

I think it is better to use tidy evaluation

library(dplyr)
library(rlang)

filter_fun <- function(df, cat, cat_val){
    df %>%
        filter({{cat}} == cat_val)
}

filter_fun(iris, Species, "setosa")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1           5.1         3.5          1.4         0.2  setosa
#> 2           4.9         3.0          1.4         0.2  setosa
#> 3           4.7         3.2          1.3         0.2  setosa
#> 4           4.6         3.1          1.5         0.2  setosa
#> 5           5.0         3.6          1.4         0.2  setosa
#> 6           5.4         3.9          1.7         0.4  setosa
#> 7           4.6         3.4          1.4         0.3  setosa
#> 8           5.0         3.4          1.5         0.2  setosa
#> 9           4.4         2.9          1.4         0.2  setosa
#> 10          4.9         3.1          1.5         0.1  setosa
#> 11          5.4         3.7          1.5         0.2  setosa
#> 12          4.8         3.4          1.6         0.2  setosa
#> 13          4.8         3.0          1.4         0.1  setosa
#> 14          4.3         3.0          1.1         0.1  setosa
#> 15          5.8         4.0          1.2         0.2  setosa
#> 16          5.7         4.4          1.5         0.4  setosa
#> 17          5.4         3.9          1.3         0.4  setosa
#> 18          5.1         3.5          1.4         0.3  setosa
#> 19          5.7         3.8          1.7         0.3  setosa
#> 20          5.1         3.8          1.5         0.3  setosa
#> 21          5.4         3.4          1.7         0.2  setosa
#> 22          5.1         3.7          1.5         0.4  setosa
#> 23          4.6         3.6          1.0         0.2  setosa
#> 24          5.1         3.3          1.7         0.5  setosa
#> 25          4.8         3.4          1.9         0.2  setosa
#> 26          5.0         3.0          1.6         0.2  setosa
#> 27          5.0         3.4          1.6         0.4  setosa
#> 28          5.2         3.5          1.5         0.2  setosa
#> 29          5.2         3.4          1.4         0.2  setosa
#> 30          4.7         3.2          1.6         0.2  setosa
#> 31          4.8         3.1          1.6         0.2  setosa
#> 32          5.4         3.4          1.5         0.4  setosa
#> 33          5.2         4.1          1.5         0.1  setosa
#> 34          5.5         4.2          1.4         0.2  setosa
#> 35          4.9         3.1          1.5         0.2  setosa
#> 36          5.0         3.2          1.2         0.2  setosa
#> 37          5.5         3.5          1.3         0.2  setosa
#> 38          4.9         3.6          1.4         0.1  setosa
#> 39          4.4         3.0          1.3         0.2  setosa
#> 40          5.1         3.4          1.5         0.2  setosa
#> 41          5.0         3.5          1.3         0.3  setosa
#> 42          4.5         2.3          1.3         0.3  setosa
#> 43          4.4         3.2          1.3         0.2  setosa
#> 44          5.0         3.5          1.6         0.6  setosa
#> 45          5.1         3.8          1.9         0.4  setosa
#> 46          4.8         3.0          1.4         0.3  setosa
#> 47          5.1         3.8          1.6         0.2  setosa
#> 48          4.6         3.2          1.4         0.2  setosa
#> 49          5.3         3.7          1.5         0.2  setosa
#> 50          5.0         3.3          1.4         0.2  setosa

Created on 2020-12-24 by the reprex package (v0.3.0.9001)

Thanks a lot.............

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.