group_by in creating function

hi I have two sets of code:

data1 <- df %>% 
filter(animal == "cat", 
color %in% c("Black", "Blue", "Green")) %>% 
group_by(color) %>% 
summarize(count = n()) 

data2 <- df %>% 
filter(animal == "cat", 
color %in% c("Black", "Blue", "Green")) %>% 
group_by(color, animal, location) %>% 
summarize(count = n()) 

as you can see the only thing different is the group. is there a way to make a function so I dont have to repeat so much code? thank u so much !!

Hello,

you can do this for sure. I included a filter_var argument, so that you can see the use of {{ for single variable isnertion. Dynamic dots are used for multiple injections.

library(dplyr)
Data <- data.frame(animal = sample(c('cat','dog','horse'),1000,TRUE,c(.6,.2,.2)),
                   color = sample(c('black','brown','white'),1000,TRUE),
                   location = sample(c('east','north','west'),1000,TRUE))

filter_group_count <- function(df,filter_var,filter_cond,...){
  df |>
    filter( {{filter_var}} == filter_cond) |>
    group_by( ... ) |>
    summarise(
      count = n()
    )
}

filter_group_count(df = Data, filter_var = animal, filter_cond = 'cat', color)
#> # A tibble: 3 × 2
#>   color count
#>   <chr> <int>
#> 1 black   197
#> 2 brown   174
#> 3 white   183
filter_group_count(df = Data, filter_var = animal, filter_cond = 'cat', color, location)
#> `summarise()` has grouped output by 'color'. You can override using the
#> `.groups` argument.
#> # A tibble: 9 × 3
#> # Groups:   color [3]
#>   color location count
#>   <chr> <chr>    <int>
#> 1 black east        62
#> 2 black north       61
#> 3 black west        74
#> 4 brown east        55
#> 5 brown north       56
#> 6 brown west        63
#> 7 white east        55
#> 8 white north       61
#> 9 white west        67

Created on 2022-10-03 by the reprex package (v2.0.1)

I think this should do what you want!

Kind regards

1 Like

{{filter_var}} == filter_cond)

is it possible to filter by multiple conditions like

 {{filter_var}} %in% filter_cond)

filter_group_count(df = Data, filter_var = animal, filter_cond = c('cat', 'dog'), color)

thank u!!

I almost also getting unused argument for what i am using to group. is there anyway to specify like group_by = color?

Regarding your first additional question: Yes, you can just modify it like you already did with %in%:

filter_group_count <- function(df,filter_var,filter_cond,...){
  df |>
    filter( {{filter_var}} %in% filter_cond) |>
    group_by( ... ) |>
    summarise(
      count = n()
    )
}

filter_group_count(df = Data, filter_var = animal, filter_cond = c('cat','dog'), animal, color)
#> `summarise()` has grouped output by 'animal'. You can override using the
#> `.groups` argument.
#> # A tibble: 6 × 3
#> # Groups:   animal [2]
#>   animal color count
#>   <chr>  <chr> <int>
#> 1 cat    black   185
#> 2 cat    brown   225
#> 3 cat    white   200
#> 4 dog    black    69
#> 5 dog    brown    59
#> 6 dog    white    69

Created on 2022-10-03 with reprex v2.0.2

I don't really understand your second question. You can pass color as argument to ... and it will group by color only. Or what exactly do you want the function to do / what are you expecting the function to do (which is doesn't do?)?

Kind regards

1 Like

oh thank you. my question is about the (...) in the group_by. this is the error I get using my own data.

error in filter_group_count(data = df, filter_var = type,  :
unused argument (color)

are there any things I can check? this is so strange...

when I run your example I get it totally the same though

You could type dput(head(Data,50)) (where Data is the name of your data.frame) and paste it into the forum. Maybe I can reproduce your error and see where the problem is, since atm I don't know what could cause the error.

This topic was automatically closed 7 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.